@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(Parameterized.class)
@SpringApplicationConfiguration(classes = {ApplicationConfigTest.class})
public class ServiceTest {
}
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
@RunWith(Parameterized.class)
@ContextConfiguration(...)
public class MyTest {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
...
@RunWith(Parameterized.class)
@ContextConfiguration(classes = {ApplicationConfigTest.class})
public class ServiceTest {
private TestContextManager testContextManager;
@Before
public void setUpContext() throws Exception {
//this is where the magic happens, we actually do "by hand" what the spring runner would do for us,
// read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though
this.testContextManager = new TestContextManager(getClass());
this.testContextManager.prepareTestInstance(this);
}
...
}
@SuppressWarnings("InstanceMethodNamingConvention")
@ContextConfiguration(classes = {ServiceTest.class})
public class SpringAwareTest {
@ClassRule
public static final SpringAware SPRING_AWARE = SpringAware.forClass(SpringAwareTest.class);
@Rule
public TestRule springAwareMethod = SPRING_AWARE.forInstance(this);
@Rule
public TestName testName = new TestName();
...
}
4条答案
按热度按时间hec6srdp1#
您可以使用Spring提供的SpringClassRule和SpringMethodRule
clj7thdc2#
至少有两个选项可以做到这一点:
1.遵循http://www.blog.project13.pl/index.php/coding/1077/runwith-junit4-with-both-springjunit4classrunner-and-parameterized/
您的测试需要看起来像这样:
1.有一个github项目https://github.com/mmichaelis/spring-aware-rule,它建立在以前的blog上,但是以一种通用的方式增加了支持
所以你可以有一个基类来实现其中的一种方法,所有的测试都从它继承。
sc4hvdpw3#
JUnit 4.12还提供了另一种解决方案,不需要Spring 4.2+。
JUnit 4.12引入了ParametersRunnerFactory,它允许将参数化测试和Spring注入相结合。
可以将工厂添加到测试类中,以给予完全的Spring支持,如test transaction、reinit dirty context和servlet test。
如果您需要Spring context inside @Parameters静态方法为测试示例提供参数,请在这里查看我的答案How can I use the Parameterized JUnit test runner with a field that's injected using Spring?。
tag5nh1u4#
自行处理应用程序上下文
对我起作用的是有一个“手动”管理应用程序上下文的
@RunWith(Parameterized.class)
测试类。为此,我创建了一个应用程序上下文,该上下文具有与
@ContextConfiguration
中相同的字符串集合。我有
对于我需要的每个@Autowired,我从创建的上下文中手动获取它:
请不要忘记在结尾处关闭上下文: