本文介绍了在集成测试期间服务未开始使用 Spring-boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 rest-assured 和 spring-boot 编写我的测试.
I am writting my tests useing rest-assured and spring-boot.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationSErvice.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")//dynamically pick up ports
public class MyTestClass{
@Value("${local.server.port}")
int port;
@Before
public void setup(){
RestAssured.port=port;
}
@Test
public void testMethod(){
//asserting
}
在日志中,我看到它说在端口上启动 Tomcat 并启动服务 Tomcat,但随后失败并出现此错误 -
In the log I see it says starting Tomcat on port and STarting service Tomcat but then it fails with this error -
ERROR 8480 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase : A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/service]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
推荐答案
对于 Web 集成测试,您应该使用 @WebIntegrationTest
代替.
For web integration testing, you should use @WebIntegrationTest
instead.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest
public class ApplicationTest {
@Value("${local.server.port}")
private int port;
@Before
public void setup() {
RestAssured.baseURI = "http://localhost:" + port;
}
@Test
public void testStatus() {
given().contentType(ContentType.JSON).get("/greeting").prettyPeek().then().statusCode(200);
}
@Test
public void testMessage() {
given().contentType(ContentType.JSON).get("/greeting").then()
.body("content", is("Hello, World!"));
}
}
这篇关于在集成测试期间服务未开始使用 Spring-boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!