自从Spring Boot Framework中不推荐使用@SpringApplicationConfiguration @WebIntegration以来,正确的注释是什么?


68

从Spring Boot Framework 1.4开始@SpringApplicationConfiguration@WebIntegration并且不推荐使用什么正确的注释?我正在尝试进行单元测试。


4
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
Ulises


它不是您问题的答案,但是,Spring boot不是框架。但这是Spring Framework的观点。springhow.com/spring-boot-and-spring
Raja Anbazhagan

Answers:


48

查看不推荐使用的类的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 {

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

是的,这里是:

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

29

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

他们描述了如下基本示例:

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

作为以下之一的替代品:

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

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

好问题,我不知道,但一次可能只是一个正在运行的应用程序。
user1767316 '17


6

您应该使用以下注释:

@ContextConfiguration(classes = main_class)

这不起作用,因为在集成测试期间我的应用无法从属性文件获取数据库URL参数。
Neeraj Jain

1
@NeerajJain只需稍作调整就可以了:@ContextConfiguration(classes = main_class,initializers = ConfigFileApplicationContextInitializer.class)
sowieso-fruehling

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.