CoffeJunky
CoffeJunky

Reputation: 1087

JUnit setUp gets invoked TWO times with one test and messing up Powermock expectNew

Very strange behaviour in my test.

  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
     }
  }

StackTrace:

  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       

Any ideas why my setUp gets called twice?

I do some mocking in the setUp and after a verify i get the expect 2 times called 1 times, thing so my tests fail.

i am using powermock 1.4.8, junit 4.4, and the easymock framework

Upvotes: 3

Views: 5130

Answers (3)

SadanandM
SadanandM

Reputation: 59

Firstly you need to make the setup method as "static". Referring to this answer here, https://stackoverflow.com/a/5978702/964446

you cannot do both extends TestCase and use annotations, as they use different runners. So when JUnit sees extends TestCase it will most probably use the old runner, where-in the @Test annotation will be overlooked.

You can simple make the setup method as static without extending the TestCase and use @Test annotations, this should work as per JUnit4

Upvotes: 0

Pace
Pace

Reputation: 43867

Does your class extend TestCase? It doesn't appear to in the example but I'm just trying to think what could be causing this. If it did then it would ignore the annotations and consider every method that starts with the word test as a test case causing you to have possibly more tests than you think.

Upvotes: 6

laz
laz

Reputation: 28638

I think you want to use @BeforeClass instead of @Before. @Before gets run before each method annotated with @Test, whereas @BeforeClass only runs once before all @Test methods.

Upvotes: 4

Related Questions