java 使用 SpringBootTest 注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49009317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:14:10  来源:igfitidea点击:

Using SpringBootTest annotation

javaspringgradlerest-assured

提问by Matt Damon

I've built an API using Spring Boot and have a few unit tests associated with it so far. As expected these are ran locally and when Jenkins builds the project via gradle buildon the build server. I'm looking to add some integration tests to the project now using rest-assured so I can actually test request and responses but I'm not overly clear how to add them to the project.

到目前为止,我已经使用 Spring Boot 构建了一个 API,并且有一些与之相关的单元测试。正如预期的那样,这些是在本地运行的,当 Jenkins 通过gradle build构建服务器构建项目时。我希望现在使用 rest-assured 向项目添加一些集成测试,这样我就可以实际测试请求和响应,但我不太清楚如何将它们添加到项目中。

So far I've added an integrationTests folder to src/test and have told gradle to exclude the rest-assured tests when gradle buildor via gradle testso I can keep them independent. I've setup a gradle task gradle integrationTestto trigger the integration tests which runs fine as long as the API is currently running.

到目前为止,我已经在 src/test 中添加了一个 integrationTests 文件夹,并告诉 gradle 在何时gradle build或通过时排除放心的测试,gradle test以便我可以保持它们独立。我已经设置了一个 gradle 任务gradle integrationTest来触发集成测试,只要 API 当前正在运行,它就可以正常运行。

Apparently I can annotate the test class with @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)to instruct spring to start up an embedded server for testing automatically but it doesn't appear to be doing anything, the tests just fail with a java.net.ConnectExceptionerror until I start the API in another window. Is there anything wrong with the below sample code or am I missing something else?

显然,我可以对测试类进行注释,@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)以指示 spring 启动嵌入式服务器进行自动测试,但它似乎没有做任何事情,测试只是失败并出现java.net.ConnectException错误,直到我在另一个窗口中启动 API。下面的示例代码有什么问题还是我错过了其他东西?

com.example.restassured;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

import static io.restassured.RestAssured.*;

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) 
public class RunSomeTestsIT {

    @Test public void url_should_return_200_response() {
       get("/v1/test/123456").then().statusCode(200);
    }

}

build.gradle

构建.gradle

task integrationTest(type: Test) {
    include 'com/example/restassured/**'
    // Run all tests each time. Do not cache.
    outputs.upToDateWhen { false } 
}

采纳答案by Peter Thomas

By co-incidence I was working on something very similar today, and hopefully can provide some useful pointers.

巧合的是,我今天正在做一些非常相似的事情,希望能提供一些有用的指导。

When I compare with this codeI think you are missing the @RunWith(SpringRunner.class)annotation !

当我与此代码进行比较时,我认为您缺少@RunWith(SpringRunner.class)注释!

Edit: other than moving your test to the same package as the "Application" class, you can try a Spring "first-principles" approach which side-steps all the black-magic of the Spring annotations. At least you can troubleshoot things better and control what's going on:

编辑:除了将测试移动到与“应用程序”类相同的包之外,您还可以尝试使用 Spring 的“第一原则”方法来回避 Spring 注释的所有黑魔法。至少你可以更好地解决问题并控制正在发生的事情:

https://github.com/ptrthomas/spring-testing/blob/master/src/test/java/example/HelloE2ERestKarateTest.java

https://github.com/ptrthomas/spring-testing/blob/master/src/test/java/example/HelloE2ERestKarateTest.java