ContextConfiguration不适用于测试平台的JUnit4类

问题描述 投票:0回答:1

您好我在使用Spring引导运行Vaadin测试用例时,找到了在@ContextConfiguration中给出实例化bean的解决方案。

这是我配置的一些代码。

@WebAppConfiguration
@RunWith(JUnit4.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = UIConfiguration.class)
public abstract class BaseTestCase extends TestBenchTestCase {
//code stuff
}

@Configuration
@PropertySource("classpath:META-INF/spring/application.properties")
@ComponentScan(basePackages = "com.ui", excludeFilters = @ComponentScan.Filter(value = Controller.class, type = FilterType.ANNOTATION))
@EnableAsync
@EnableI18N
public class UIConfiguration implements AsyncConfigurer {
//block of code
}
unit-testing spring-boot automated-tests spring-boot-test vaadin8
1个回答
0
投票

如果你不想使用ClassRule,你应该可以使用RuleSpringRunner Spring提供

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

有关更多详细信息,请参阅java doc

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/junit4/rules/SpringClassRule.html

SpringClassRule是一个自定义的JUnit TestRule,它通过TestContextManager和相关的支持类和注释在标准JUnit测试中支持Spring TestContext Framework的类级功能。

与SpringJUnit4ClassRunner相比,Spring基于规则的JUnit支持的优势在于它独立于任何Runner,因此可以与现有的替代运行程序(如JUnit的Parameterized或第三方运行程序,如MockitoJUnitRunner)结合使用。

但是,为了实现与SpringJUnit4ClassRunner相同的功能,SpringClassRule必须与SpringMethodRule结合使用,因为SpringClassRule仅支持SpringJUnit4ClassRunner的类级功能。

© www.soinside.com 2019 - 2024. All rights reserved.