Java 什么是正确的注释,因为 @SpringApplicationConfiguration、@WebIntegration 在 Spring Boot 框架中已被弃用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39417530/
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-08-11 21:07:27  来源:igfitidea点击:

What is the proper annotation since @SpringApplicationConfiguration, @WebIntegration, is deprecated in Spring Boot Framework?

javaspring-bootjunitspring-annotations

提问by Lisa

What is the proper annotation since @SpringApplicationConfigurationand @WebIntegrationare deprecated as of Spring Boot Framework 1.4? I'm trying to play around with unit testing.

什么是正确的注释,因为@SpringApplicationConfiguration@WebIntegration被弃用春天引导框架1.4的?我正在尝试进行单元测试。

回答by Artem Bilan

Take a look into JavaDocs of deprecated classes:

查看已弃用类的 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 {


* @deprecated as of 1.4 in favor of {@link SpringBootTest} or direct use of
* {@link SpringBootContextLoader}.
*/
...
@Deprecated
public @interface SpringApplicationConfiguration {

Is there also a replacement for TestRestTemplate()?

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

Yes, here it is:

是的,这里是:

 * @deprecated as of 1.4 in favor of
 * {@link org.springframework.boot.test.web.client.TestRestTemplate}
 */
@Deprecated
public class TestRestTemplate extends RestTemplate {

回答by satish chennupati

you can use @EnableAutoConfiguration or @SpringBootApplication.

您可以使用@EnableAutoConfiguration 或@SpringBootApplication。

for testing purpose you can use @SpringBootTest(webEnvironment='your value') or simply @SpringBootTest

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

please refer :

请参考:

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

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

for testing the REST, you can use @RestClientTest and configure a RestTemplateBuilder.

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

回答by Grigore Chis

You should use this annotation:

您应该使用此注释:

@ContextConfiguration(classes = main_class)

回答by user1767316

A good place to start is now probably: Testing improvements in Spring Boot 1.4.

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

They describe a basic sample like the following:

他们描述了一个基本示例,如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyTest {
}

as a replacement to, one of many:

作为众多替代之一:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApp.class)
@WebIntegrationTest
public class MyTest {
}