什么是正确的注释,因为在Spring Boot Framework中不推荐使用@SpringApplicationConfiguration,@ WebIntegration?

Lis*_*isa 61 java junit spring-annotations spring-boot

从Spring Boot Framework 1.4 开始@SpringApplicationConfiguration,什么是正确的注释并被@WebIntegration弃用?我正试图玩单元测试.

Art*_*lan 47

查看已弃用类的JavaDocs:

* @deprecated as of 1.4 in favor of
 * {@link org.springframework.boot.test.context.SpringBootTest} with
 * {@code webEnvironment=RANDOM_PORT} or {@code webEnvironment=DEFINED_PORT}.
 */
...
@Deprecated
public @interface WebIntegrationTest {
Run Code Online (Sandbox Code Playgroud)
* @deprecated as of 1.4 in favor of {@link SpringBootTest} or direct use of
* {@link SpringBootContextLoader}.
*/
...
@Deprecated
public @interface SpringApplicationConfiguration {
Run Code Online (Sandbox Code Playgroud)

是否还有TestRestTemplate()的替代品?

是的,这是:

 * @deprecated as of 1.4 in favor of
 * {@link org.springframework.boot.test.web.client.TestRestTemplate}
 */
@Deprecated
public class TestRestTemplate extends RestTemplate {
Run Code Online (Sandbox Code Playgroud)


use*_*316 25

现在可能是一个很好的起点: 测试Spring Boot 1.4中的改进.

他们描述了如下基本样本:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyTest {
}
Run Code Online (Sandbox Code Playgroud)

作为许多人之一的替代品:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApp.class)
@WebIntegrationTest
public class MyTest {
}
Run Code Online (Sandbox Code Playgroud)

  • 你的上层版本如何知道ApplicationConfiguration在MyApp.class类中?当我以你的方式实现它时,它无法加载applicationContext (2认同)

sat*_*ati 9

您可以使用@EnableAutoConfiguration或@SpringBootApplication.

出于测试目的,您可以使用@SpringBootTest(webEnvironment ='您的值')或简单地使用@SpringBootTest

请参考:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

为了测试REST,您可以使用@RestClientTest并配置RestTemplateBuilder.


小智 5

您应该使用以下注释:

@ContextConfiguration(classes = main_class)
Run Code Online (Sandbox Code Playgroud)