问题描述
我有一个看起来像的测试类
I have a test class that looks like
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/test-context.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public abstract class TestClass {
@Rule @Resource public JUnitRuleMockery jMockContext;
public void test1() {
//Expectations and test
}
public void test2() {
//Expectations and test
}
}
在test-context.xml
中,我定义JUnitRuleMockery
以及通过factory-method
的几个模拟对象,例如
and in test-context.xml
I define the JUnitRuleMockery
plus several mock objects through a factory-method
, like
<bean id="mockContextFactory" class="MockContextFactory" />
<bean id="jMockContext" factory-bean="mockContextFactory" factory-method="getContext" scope="prototype" />
<bean id="firstMock" factory-bean="mockContextFactory" factory-method="getFirstMock" />
<bean id="secondMock" factory-bean="mockContextFactory" factory-method="getSecondMock" />
MockContextFactory
是
public class MockContextFactory
{
private JUnitRuleMockery jUnitRuleMockery;
public MockContextFactory() {
jUnitRuleMockery = new JUnitRuleMockery();
jUnitRuleMockery.setThreadingPolicy(new Synchroniser());
}
public JUnitRuleMockery getContext() {
return jUnitRuleMockery;
}
public FirstMock getFirstMock() {
return jUnitRuleMockery.mock(FirstMock.class);
}
//others getter
}
在TestClass
中,我有几种测试方法,由于有注释@DirtiesContext
,我希望在每次测试执行后重新加载Spring上下文(由于每个测试都对模拟对象设置了期望,因此我必须重新加载Spring上下文).请从此处查看 @DirtiesContext .但是,似乎没有重新加载Spring上下文:实际上,在test2
的开头进入调试模式(假定test1
已经执行过),我可以看到jMockContext
仍然保留期望,执行列表和错误(如果有的话)来自test1
.
因此,最后有几个问题,@DirtiesContext
确实会导致重新加载Spring上下文(如我从Spring Docs所了解的那样),还是我误解了注释?在第一种情况下,我在做什么错?在后一种情况下,如何强制为每个测试重新加载Spring上下文?
In TestClass
I have several test methods and, due to the annotations @DirtiesContext
, I am expecting the Spring context to be reloaded after each test execution (since each test sets expectations on mock objects, I have to reload Spring context every time). See @DirtiesContext from here. However, it appears that the Spring context is not reloaded: in fact, entering in debug mode at the beginning of test2
(supposedly test1
has been executed earlier) I can see jMockContext
still holding expectations, execution list and errors (if any) from test1
.
So, to end up with few questions, does @DirtiesContext
really cause Spring context to be reloaded (as I understood from Spring Docs) or did I misunderstand the annotation? In the first case, what am I doing wrong? In the latter case, how can I force Spring context to be reloaded for every test?
编辑,以界定问题:我的代码像上面的示例从几天开始运行,然后今天我创建了一个新的测试,其中添加了一个失败的期望.然后,我看到该类中的所有其他测试由于相同的原因而失败(直到今天它们仍在green
位置).调试时,我发现jMockContext
从未清除,这意味着所有测试都将期望添加到同一池中:当然,只要没有期望失败,它就是透明的,并且我没有注意到(test1
添加和通过很少的期望,test2
接受了一个不空的期望池,并且已经通过并添加了自己的期望,所以没有问题),但是我不喜欢以前的情况,并且我想在没有先前设定的期望的情况下开始每个测试全部.
EDIT, to delimit the problem: I had a code like the sample above running from few days, then today I created a new test in which I added an expectation which failed. Then, I saw all the other tests in the class failing for the same reason (while they where green
until today). Debugging, I found out that jMockContext
was never cleared, which means all tests were adding expectations to the same pool: of course, as long as no expectation failed, that was transparent and I did not notice it (test1
adds and passes few expectations, test2
takes a not-empty pool of expectations ALREADY passed and adds its own so there was no problem), but I do not like the previous situation and I would like to start each test with no previous set expectations at all.
推荐答案
这是我的解决方案.我不知道这是否仅限于Spring 3.2或是否是一个普遍的误解,但仅使用@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
不足以导致为每个测试重新加载整个Spring上下文.我还必须在@TestExecutionListeners
中添加DirtiesContextTestExecutionListener
.
因此,最后,对我有用的只是将TestClass
的注释更改为
Here is my solution. I do not know if this is limited to Spring 3.2 or if it is a general misunderstanding, but simply using @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
is not enough to cause the entire Spring context to be reloaded for each test. I had to add also the DirtiesContextTestExecutionListener
in @TestExecutionListeners
.
So, in the end, what worked for me was just to change the annotation of my TestClass
to
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/test-context.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@TestExecutionListeners({DirtiesContextTestExecutionListener.class})
public abstract class TestClass
没有其他改变.
这篇关于Spring @DirtiesContext是否重新加载Spring上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!