问题描述
为什么我的单元测试在独立运行时通过,但是在运行多个测试时却失败?
Why are my unit-tests passing when run independently, but fail when running multiple tests?
当我执行单个单元测试时,我的测试将成功模拟并返回预期结果.但是,当我运行所有单元测试时,以前通过的测试将失败.
When I execute a single unit test, my tests will successfully mock and return the expected results. However, when I run all unit-tests my previously passing test will fail.
一次测试运行
多次测试
shouldDoThisAgain()-失败
shouldDoThisAgain() - Fail
shouldDoThisAgainAgain()-失败
shouldDoThisAgainAgain() - Fail
我的测试:
@PrepareForTest({OtherMethods.class})
@PowerMockIgnore("javax.management.*")
@RunWith(PowerMockRunner.class)
public class DbTest {
@Test
public void shouldDoThis() throws Exception() {
Dal dalMock = mock(Dal.class)
PowerMockito.whenNew(Dal.class).withAnyArguments().thenReturn(dalMock)
List<Result> results = new ArrayList<Result>();
results.add(new Result(1,2,3));
when(dalMock.getResults()).thenReturn(results)
assertTrue(Wrapper.MY_WRAPPER.run());
}
@Test
public void shouldDoThisAgain() throws Exception() {
Dal dalMock = mock(Dal.class)
PowerMockito.whenNew(Dal.class).withAnyArguments().thenReturn(dalMock)
List<Result> results = new ArrayList<Result>();
results.add(new Result(2,3,4));
when(dalMock.getResults()).thenReturn(results)
assertTrue(Wrapper.MY_WRAPPER.run());
}
@Test
public void shouldDoThisAgainAgain() throws Exception() {
Dal dalMock = mock(Dal.class)
PowerMockito.whenNew(Dal.class).withAnyArguments().thenReturn(dalMock)
List<Result> results = new ArrayList<Result>();
results.add(new Result(6,5,3));
when(dalMock.getResults()).thenReturn(results)
assertTrue(Wrapper.MY_WRAPPER.run());
}
}
我的课程:
public class Wrapper {
// not Runnable
public static final MyWrapper MY_WRAPPER = new MyWrapper(...){
@Override
public boolean run() {
// returns empty list when the test is alone
// returns 'results' variable when ran with other tests alone
List<Result> results = OtherMethods.getDal().getResults();
return !results.isEmpty()
}
};
}
public class OtherMethods {
private static final Logger LOGGER = LogManager.getLogger(OtherMethods.class);
public static Dal dal;
static Dal getDal() {
if (dal == null) {
try {
dal = new Dal();
} catch (Exception e) {
LOGGER.fatal("DB Connection could not be created for Geonames");
LOGGER.fatal(e);
}
}
return dal;
}
}
推荐答案
我找到了我们项目的解决方案.我编写了一个Logger类,该类调用Android的内部静态Log方法.我的某些测试没有直接测试Log类.当我忽略所有这些选项时,基于powermockito的测试变为绿色.但是,当运行这些其他测试时,基于powermockito的测试将失败.有时.
I found the solution for our project. I wrote a Logger class that calls Android's internal static Log methods. Some of my tests did not directly test the Log class. When I ignored all of them, the powermockito-based tests went green. But when these other tests were run, the powermockito-based ones would fail. Sometimes.
这种方法会失败(片状):
This approach would fail (flaky):
@RunWith(PowerMockRunner.class)
@PrepareForTest({Log.class}) // WARNING: HERE BE DRAGONS!
public class MyTest {
@Test
public void testMethodThatDoesNotUseStatics() {
// ...
}
@Test
public void usesStatics() {
// ...
}
}
然后我发现您可以使用@PrepareForTest
注释每个测试方法,如下所示:
Then I found out you can annotate each test method with @PrepareForTest
, like this:
@RunWith(PowerMockRunner.class)
public class MyTest {
@Test
public void testMethodThatDoesNotUseStatics() {
// ...
}
@Test
@PrepareForTest({Log.class}) // that's the way :)
public void usesStatics() {
// ...
}
}
现在测试再次变为绿色.是的,用于非片状测试! :)
Now the tests are green again. Yay for non-flaky tests! :)
这篇关于PowerMock测试通过然后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!