1、java.lang.IllegalStateException: You haven't configured a MockMVC instance. You can do this statically
在服务提供方,执行gradle test后,契约的validate失败,错误信息如下图:
It turns out that I need to add the base class that I am missing(like the FraudBase.java) in the sample. That's where the MockMvc is being instantiated.
https://stackoverflow.com/questions/42405945/spring-cloud-contract-generated-test-doesnt-have-mockmvc-configured-and-fails
第一步:我加了一个base class如下:
package com.xxx.xxx.contract; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.context.WebApplicationContext; import com.xxx.xxx.Application; import io.restassured.module.mockmvc.RestAssuredMockMvc; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) //@ActiveProfiles("unit-test") //当契约测试时想使用不同的配置文件时,取消注释时会使用loan-unit-test.yml配置文件 public abstract class ConstractTestBase { static{ System.setProperty("aes.xxx", "abc"); } @Autowired private WebApplicationContext context; @Before public void setUp() throws Exception { RestAssuredMockMvc.webAppContextSetup(context); } }
第二步:在gradle脚本中,指定
contracts { baseClassForTests = 'com.xxx.xxx.contract.ConstractTestBase'
}