Spring 运行器调用“@Before”方法时不会自动装配字段
Spring doesn't autowire fields when runner invokes '@Before' method
我有以下测试:
@RunWith(Parameterized.class)
@SpringBootTest
@ContextConfiguration(classes = MyConfig.class)
public class OrderPlacementCancelTest {
@Autowired
private TestSessionsHolderPerf sessionsHolder;
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
private CommonCredentialSetter commonCredentialSetter;
@Before
public void login() throws InterruptedException {
int attempts = 0;
while (!sessionsHolder.isClientLoggedIn() && attempts < 3) {
Thread.sleep(1000);
++attempts;
}
}
@Parameterized.Parameters()
public static Collection<Object[]> data() {
...
}
及以下参赛者:
@Test
public void test() throws Exception {
Class[] cls = {OrderPlacementCancelTest.class};
Result result = JUnitCore.runClasses(new ParallelComputer(false, true), cls);
logger.info("Failure count={}", result.getFailureCount());
for (Failure failure : result.getFailures()) {
logger.error(failure.getTrace());
}
}
当我通过 runner 开始测试时,我看到 有时 标记为 @Before
的方法 login
抛出 NullPointerException
因为 sessionsHolder 为空。
如何避免?
看来 @Parameterized
与 junit 规则不能很好地融合。一种选择是通过在参数化测试 class 的构造函数中执行逻辑来替换 Rule
,或者在测试方法的开头调用设置方法。
在任何一种情况下,您都可以使用 TestContextManager
使用 @AutoWired
值填充您的测试 class,如下所示:
private TestContextManager testContextManager;
@Before
public void login() throws Exception {
testContextManager = new TestContextManager(getClass());
testContextManager.prepareTestInstance(this);
我有以下测试:
@RunWith(Parameterized.class)
@SpringBootTest
@ContextConfiguration(classes = MyConfig.class)
public class OrderPlacementCancelTest {
@Autowired
private TestSessionsHolderPerf sessionsHolder;
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
private CommonCredentialSetter commonCredentialSetter;
@Before
public void login() throws InterruptedException {
int attempts = 0;
while (!sessionsHolder.isClientLoggedIn() && attempts < 3) {
Thread.sleep(1000);
++attempts;
}
}
@Parameterized.Parameters()
public static Collection<Object[]> data() {
...
}
及以下参赛者:
@Test
public void test() throws Exception {
Class[] cls = {OrderPlacementCancelTest.class};
Result result = JUnitCore.runClasses(new ParallelComputer(false, true), cls);
logger.info("Failure count={}", result.getFailureCount());
for (Failure failure : result.getFailures()) {
logger.error(failure.getTrace());
}
}
当我通过 runner 开始测试时,我看到 有时 标记为 @Before
的方法 login
抛出 NullPointerException
因为 sessionsHolder 为空。
如何避免?
看来 @Parameterized
与 junit 规则不能很好地融合。一种选择是通过在参数化测试 class 的构造函数中执行逻辑来替换 Rule
,或者在测试方法的开头调用设置方法。
在任何一种情况下,您都可以使用 TestContextManager
使用 @AutoWired
值填充您的测试 class,如下所示:
private TestContextManager testContextManager;
@Before
public void login() throws Exception {
testContextManager = new TestContextManager(getClass());
testContextManager.prepareTestInstance(this);