运行 Spring 非 Spring 项目中的云合同测试
Running Spring Cloud Contract test in non-Spring project
我在 Spring 引导项目 (spring-server
) 中创建了一个 Spring 云合同存根。想要调用此存根的客户端不是 Spring 项目,也不能是一个。如果我 运行 客户端中的以下内容:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureStubRunner(ids = {"uk.co.hadoopathome:spring-server:+:stubs:8093"},
stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class ContractTest {
@Test
public void testContractEndpoint() {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:8093/ac01");
CloseableHttpResponse response = httpclient.execute(httpGet);
String entity = EntityUtils.toString(response.getEntity());
assertEquals("ac01returned", entity);
response.close();
} catch (IOException ignored) {
}
}
}
然后我得到一个错误
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
显然我没有 @SpringBootConfiguration
,因为这不是 Spring 引导项目。
这里有什么解决方法?
我修改了 @SpringBootTest
行:
@SpringBootTest(classes = ContractTest.class)
然后遇到一些 Logback 错误,我通过 finding this answer 解决了这些错误并添加到 build.gradle
:
configurations {
all*.exclude module : 'spring-boot-starter-logging'
}
只需使用 Junit 规则,您就不必设置上下文
public class JUnitTest {
@Rule public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example","beer-api-producer")
.withPort(6543)
.workOffline(true);
@Test
public void should_work() {
String response = new RestTemplate().getForObject("http://localhost:6543/status", String.class);
BDDAssertions.then(response).isEqualTo("ok");
}
我在 Spring 引导项目 (spring-server
) 中创建了一个 Spring 云合同存根。想要调用此存根的客户端不是 Spring 项目,也不能是一个。如果我 运行 客户端中的以下内容:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureStubRunner(ids = {"uk.co.hadoopathome:spring-server:+:stubs:8093"},
stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class ContractTest {
@Test
public void testContractEndpoint() {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:8093/ac01");
CloseableHttpResponse response = httpclient.execute(httpGet);
String entity = EntityUtils.toString(response.getEntity());
assertEquals("ac01returned", entity);
response.close();
} catch (IOException ignored) {
}
}
}
然后我得到一个错误
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
显然我没有 @SpringBootConfiguration
,因为这不是 Spring 引导项目。
这里有什么解决方法?
我修改了 @SpringBootTest
行:
@SpringBootTest(classes = ContractTest.class)
然后遇到一些 Logback 错误,我通过 finding this answer 解决了这些错误并添加到 build.gradle
:
configurations {
all*.exclude module : 'spring-boot-starter-logging'
}
只需使用 Junit 规则,您就不必设置上下文
public class JUnitTest {
@Rule public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example","beer-api-producer")
.withPort(6543)
.workOffline(true);
@Test
public void should_work() {
String response = new RestTemplate().getForObject("http://localhost:6543/status", String.class);
BDDAssertions.then(response).isEqualTo("ok");
}