Last active
April 10, 2020 19:45
-
-
Save bdemers/18a5de82a10dab43054ebbe3ae7a3bdc to your computer and use it in GitHub Desktop.
TestNG + Mockito + PowerMock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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