title: My tests don't work after switching between JUnit to TestNG
body: <p>I can't understand why my code can't execute TestNG tests. At the same time when I use JUnit test (with corresponding JUnit dependencies) instead of TestNG all works fine. Even when I recreated in my IDEA all test classes with proper test framework (TestNG) it doesn't work well. Actually the problem is in <code>PowerMock</code>.</p>
  
  <p>Here is my simple code for testing</p>
  
  <pre><code>public class Employee {
      public static int count(){
          throw new UnsupportedOperationException();
      }
  }
  </code></pre>
  
  <p>My service class under test</p>
  
  <pre><code>public class EmployeeService {
     public int getEmployeeCount(){
         return Employee.count();
     }
  }
  </code></pre>
  
  <p>And finally my test code</p>
  
  <pre><code>@PrepareForTest(Employee.class)
  public class EmployeeServiceTestNGTest {
  
      @Test
      public void should_return_count_of_employee_using_the_domain_class() {
          PowerMockito.mockStatic(Employee.class);
          PowerMockito.when(Employee.count()).thenReturn(1000);
  
          EmployeeService employeeService = new EmployeeService();
          Assert.assertEquals(1000, employeeService.getEmployeeCount());
      }
  }
  </code></pre>
  
  <p>And my pom.xml with all dependencies:</p>
  
  <pre><code>    &lt;properties&gt;
          &lt;powermock.version&gt;1.5.5&lt;/powermock.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-module-testng&lt;/artifactId&gt;
              &lt;version&gt;${powermock.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-api-mockito&lt;/artifactId&gt;
              &lt;version&gt;${powermock.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-module-testng-common&lt;/artifactId&gt;
              &lt;version&gt;1.4.11&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>
  
  <p>My stacktrace</p>
  
  <pre><code>java.lang.UnsupportedOperationException
      at il.arri.powermock.example.Employee.count(Employee.java:9)
      at il.arri.powermock.example.EmployeeServiceTestNGTest.should_return_count_of_employee_using_the_domain_class(EmployeeServiceTestNGTest.java:14)
  </code></pre>
  
  <p>UPD</p>
  
  <p>I changed my test class to</p>
  
  <p>@PrepareForTest(Employee.class)
  public class EmployeeServiceTestNGTest extends PowerMockTestCase {</p>
  
  <pre><code>@Test
  public void should_return_count_of_employee_using_the_domain_class() {
      PowerMockito.mockStatic(Employee.class);
      PowerMockito.when(Employee.count()).thenReturn(1000);
  
      EmployeeService employeeService = new EmployeeService();
      Assert.assertEquals(1000, employeeService.getEmployeeCount());
  }
  
  @ObjectFactory
  public IObjectFactory getObjectFactory() {
      return new PowerMockObjectFactory();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>But it still has an error:</p>
  
  <pre><code>org.testng.TestNGException: 
  An error occurred while instantiating class il.arri.powermock.example.EmployeeServiceTestNGTest: null
      at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:398)
      at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
      at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:115)
      at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:200)
      at org.testng.internal.TestNGClassFinder.&lt;init&gt;(TestNGClassFinder.java:120)
      at org.testng.TestRunner.initMethods(TestRunner.java:409)
      at org.testng.TestRunner.init(TestRunner.java:235)
      at org.testng.TestRunner.init(TestRunner.java:205)
      at org.testng.TestRunner.&lt;init&gt;(TestRunner.java:160)
      at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
      at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
      at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:575)
      at org.testng.SuiteRunner.init(SuiteRunner.java:159)
      at org.testng.SuiteRunner.&lt;init&gt;(SuiteRunner.java:113)
      at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
      at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
      at org.testng.TestNG.run(TestNG.java:1057)
      at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
      at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  Caused by: java.lang.NullPointerException
      at org.powermock.core.classloader.MockClassLoader.addClassesToModify(MockClassLoader.java:133)
      at org.powermock.modules.testng.internal.PowerMockClassloaderObjectFactory.newInstance(PowerMockClassloaderObjectFactory.java:81)
      at org.powermock.modules.testng.PowerMockObjectFactory.newInstance(PowerMockObjectFactory.java:42)
      at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
      ... 26 more
  </code></pre>
  
  <p>UPD (Solution):</p>
  
  <p>When I removed this dependency</p>
  
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-module-testng-common&lt;/artifactId&gt;
              &lt;version&gt;1.4.11&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>All works green :)</p>