Skip to content

Instantly share code, notes, and snippets.

@bdemers
Last active April 10, 2020 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdemers/18a5de82a10dab43054ebbe3ae7a3bdc to your computer and use it in GitHub Desktop.
Save bdemers/18a5de82a10dab43054ebbe3ae7a3bdc to your computer and use it in GitHub Desktop.
TestNG + Mockito + PowerMock
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.testng.PowerMockObjectFactory
import org.testng.IObjectFactory
import org.testng.annotations.ObjectFactory
import org.testng.annotations.Test
import static org.mockito.Mockito.*
import static org.powermock.api.mockito.PowerMockito.mockStatic
@PrepareForTest(AStaticClass.class)
public class SomeTest {
@ObjectFactory
IObjectFactory getObjectFactory() {
return new PowerMockObjectFactory()
}
@Test
public void myTestMethod {
mockStatic(AStaticClass)
when(AStaticClass.myMethod()).thenReturn(stuff)
// then use
assertThat(AStaticClass.myMethod(), is(stuff))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment