在非Spring项目中运行Spring Cloud Contract测试

时间:2018-04-27 13:35:44

标签: java spring spring-boot spring-cloud-contract

我在Spring Boot项目(spring-server)中创建了一个Spring Cloud Contract存根。想要调用此存根的客户端不是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项目。

这里的解决方法是什么?

2 个答案:

答案 0 :(得分:1)

只需使用Junit规则,您就不必设置上下文

5

答案 1 :(得分:0)

我修改了@SpringBootTest行:

@SpringBootTest(classes = ContractTest.class)

然后得到了一些Logback错误,我通过finding this answer解决并添加到build.gradle

configurations {
    all*.exclude module : 'spring-boot-starter-logging'
}