Aks
Aks

Reputation: 184

PowerMock and Mockito not working for TestNG

I have a utility class with a static method

public class A {
    public static boolean test1(){
           // Do something
           return true;
    }       
}

I am trying to mock test1 using Powermockito and using TestNG for testing

@PrepareForTest(A.class)
public class UnitTest{

   @Test
   public void testTest1() {
      PowerMockito.mockStatic(A.class);
      when(A.test1()).thenReturn(false);
   }
}

https://code.google.com/p/powermock/wiki/TestNG_usage Describes me to do this way.

However, in "when(A.test1()).thenReturn(false);" it calls the actual method test1() during the Mockito.when setup for test1() method. Hence, I believe the setup is not done right where it cannot recognize Class A as a Mock

Am I doing something wrong here?

My dependencies in pom.xml -

<dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-api-mockito</artifactId>
   <scope>test</scope>
   <version>1.6.2</version>
</dependency>
<dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-module-testng</artifactId>
   <version>1.6.2</version>
   <scope>test</scope>
</dependency>

Upvotes: 2

Views: 1915

Answers (1)

Aks
Aks

Reputation: 184

After the comment from @Damien Beaufils, I tried to google more and finally found a post in the powermock issues; The google group describing the same problem.

The solution is that your test should extend PowerMockTestCase (which is imported from the testng powermock module i.e org.powermock.modules.testng)

More info - code.google.com/p/powermock/issues/detail?id=54#c9

Upvotes: 1

Related Questions