java - 在非 Spring 项目中运行 Spring Cloud Contract 测试
问题描述
我在 Spring Boot 项目 ( spring-server
) 中创建了 Spring Cloud Contract 存根。想要调用此存根的客户端不是 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 Boot 项目。
这里有什么解决方法?
解决方案
只需使用 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");
}
推荐阅读
- c++ - 函数返回一个损坏的数组
- swift - xcode 在每个新项目中都包含旧的 ViewController
- r - 尝试登录网页时出现 Rvest 404 警告
- microsoft-graph-api - 使用 OneDrive Graph api 发送 html 电子邮件
- nsis - 安装两个应用程序,但第二个应用程序在浏览目录时获得第一个应用程序名称
- java - 我的 Xposed 模块无法从 SharePreference 中获取键和值,但是当我将它放入 Xsharepreferences 时它可以工作
- deep-learning - 如何在 Detectron2 中使用 DefaultTrainer 保存模型?
- nested - 返回错误结果的计算
- c++ - 内部格式为 GL_RGBA8 的纹理在片段着色器中显示为浮点数
- javascript - 是否可以在不升级 Meteor 的情况下升级 Meteor 使用的底层 Node.js 版本?