2

在我的测试中非常奇怪的行为。

  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 框架

4

2 回答 2

5

您的课程是否扩展了 TestCase?在示例中似乎没有,但我只是想想想可能是什么原因造成的。如果是这样,那么它将忽略注释并将以单词test开头的每个方法都视为测试用例,从而导致您进行的测试可能比您想象的要多。

于 2011-05-12T13:28:48.300 回答
4

我认为您想使用@BeforeClass而不是@Before. @Before在使用 注释的每个方法之前运行@Test,而@BeforeClass在所有方法之前只运行一次@Test

于 2011-05-12T13:22:31.590 回答