在我的测试中非常奇怪的行为。
public class MyTestclass {
@Before
void setUp(){
//do some setup, but hu i get called twice
//here i do some try catch thing to get the stacktrace...
}
void testOnlyOneTest(){
//make the testing, i get called only once
}
@After
void tearDown(){
//do some destroy things,... i get called twice too
}
}
堆栈跟踪:
1)
MyTestClassTest.setUp() line: 85
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25
Method.invoke(Object, Object...) line: 597
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner(MethodRoadie).runBefores() line: 129
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner(MethodRoadie).runBeforesThenTestThenAfters(Runnable) line: 93
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(Method, Object, Runnable) line: 294
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(Runnable) line: 282
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner(MethodRoadie).runTest() line: 84
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner(MethodRoadie).run() line: 49
PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(Method, RunNotifier) line: 207
PowerMockJUnit44RunnerDelegateImpl.runMethods(RunNotifier) line: 146
PowerMockJUnit44RunnerDelegateImpl$1.run() line: 120
ClassRoadie.runUnprotected() line: 34
ClassRoadie.runProtected() line: 44
PowerMockJUnit44RunnerDelegateImpl.run(RunNotifier) line: 118
JUnit4TestSuiteChunkerImpl.run(RunNotifier) line: 102
PowerMockRunner(AbstractCommonPowerMockRunner).run(RunNotifier) line: 53
JUnit4TestClassReference(JUnit4TestReference).run(TestExecution) line: 46
TestExecution.run(ITestReference[]) line: 38
RemoteTestRunner.runTests(String[], String, TestExecution) line: 467
RemoteTestRunner.runTests(TestExecution) line: 683
RemoteTestRunner.run() line: 390
RemoteTestRunner.main(String[]) line: 197
2)
MyTestClassTest.setUp() line: 85
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25
Method.invoke(Object, Object...) line: 597
WhiteboxImpl.performMethodInvocation(Object, Method, Object...) line: 2014
WhiteboxImpl.doInvokeMethod(Object, Class<?>, String, Object...) line: 885
WhiteboxImpl.invokeMethod(Object, String, Object...) line: 713
Whitebox.invokeMethod(Object, String, Object...) line: 401
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod() line: 305
MethodRoadie$2.run() line: 86
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner(MethodRoadie).runBeforesThenTestThenAfters(Runnable) line: 94
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(Method, Object, Runnable) line: 294
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(Runnable) line: 282
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner(MethodRoadie).runTest() line: 84
PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner(MethodRoadie).run() line: 49
PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(Method, RunNotifier) line: 207
PowerMockJUnit44RunnerDelegateImpl.runMethods(RunNotifier) line: 146
PowerMockJUnit44RunnerDelegateImpl$1.run() line: 120
ClassRoadie.runUnprotected() line: 34
ClassRoadie.runProtected() line: 44
PowerMockJUnit44RunnerDelegateImpl.run(RunNotifier) line: 118
JUnit4TestSuiteChunkerImpl.run(RunNotifier) line: 102
PowerMockRunner(AbstractCommonPowerMockRunner).run(RunNotifier) line: 53
JUnit4TestClassReference(JUnit4TestReference).run(TestExecution) line: 46
TestExecution.run(ITestReference[]) line: 38
RemoteTestRunner.runTests(String[], String, TestExecution) line: 467
RemoteTestRunner.runTests(TestExecution) line: 683
RemoteTestRunner.run() line: 390
RemoteTestRunner.main(String[]) line: 197
任何想法为什么我的 setUp 被调用两次?
我在设置中进行了一些模拟,经过验证后,我得到了预期的 2 次调用 1 次,所以我的测试失败了。
我正在使用 powermock 1.4.8、junit 4.4 和 easymock 框架