Java 既然在Spring Boot Framework中不推荐使用@SpringApplicationConfiguration、@WebIntegration,那么正确的注释是什么?
自Spring Boot Framework 1.4以来,Java 既然在Spring Boot Framework中不推荐使用@SpringApplicationConfiguration、@WebIntegration,那么正确的注释是什么?,java,spring-boot,junit,spring-annotations,Java,Spring Boot,Junit,Spring Annotations,自Spring Boot Framework 1.4以来,@SpringApplicationConfiguration和@WebIntegration被弃用,正确的注释是什么?我正在尝试单元测试。看看不推荐类的Javadoc: * @deprecated as of 1.4 in favor of * {@link org.springframework.boot.test.context.SpringBootTest} with * {@code webEnvironment=RANDO
@SpringApplicationConfiguration
和@WebIntegration
被弃用,正确的注释是什么?我正在尝试单元测试。看看不推荐类的Javadoc:
* @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 {
是否还有TestRestTemplate()的替代品 是的,在这里:
* @deprecated as of 1.4 in favor of
* {@link org.springframework.boot.test.web.client.TestRestTemplate}
*/
@Deprecated
public class TestRestTemplate extends RestTemplate {
您可以使用@EnableAutoConfiguration或@springbootplication 出于测试目的,您可以使用 @SpringBootTest(webEnvironment='your value')或简单地@SpringBootTest 请参阅:
要测试其余部分,可以使用@RestClientTest并配置RestTemplateBuilder。您应该使用以下注释:
@ContextConfiguration(classes = main_class)
一个好的起点可能是: 它们描述了如下基本示例:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyTest {
}
作为一个替代品,许多:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApp.class)
@WebIntegrationTest
public class MyTest {
}
我一直在使用以下软件,没有任何问题:
@ContextConfiguration(classes = Application.class)
然而,应用程序是我的主类的名称。@SpringBootTest(webEnvironment=webEnvironment.MOCK)看一看,这并不是对你问题的回答,但springboot不是一个框架。但这是对Spring框架的一种固执己见的观点。您的更高版本如何知道应用程序配置位于MyApp.class类中?当我以你的方式实现它时,它无法加载applicationContextGood问题,我不知道,但一次可能只有一个正在运行的应用程序。这不起作用,因为在集成测试期间,我的应用程序无法从属性文件中获取数据库URL参数。@NeerajJain只需稍加调整即可获取它们:@ContextConfiguration(classes=main_class,initializers=ConfigFileApplicationContextInitializer.class)
@ContextConfiguration(classes = Application.class)