Skip to content

Instantly share code, notes, and snippets.

@jpbriend
Created June 11, 2013 08:25
Show Gist options
  • Save jpbriend/5755273 to your computer and use it in GitHub Desktop.
Save jpbriend/5755273 to your computer and use it in GitHub Desktop.
Unit Tests with Powermock and Fest-Assert 1.x. Using mockStatic
...
...
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>1.4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
...
...
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticTokenUtils.class)
@PowerMockIgnore({ "org.xml.*", "javax.xml.*" })
public class UnitTest {
...
...
@Mock
private AppliDAO appliDAO;
@InjectMocks
private AppliServiceImpl tested = new AppliServiceImpl();
@Before
public void setUp() {
PowerMockito.mockStatic(StaticTokenUtils.class);
when(StaticTokenUtils.someStaticMethod()).thenReturn("someResult");
}
@Test
public void testMethod() throws Exception {
when(...).thenReturn(...);
Object result = tested.method("someParameter");
assertThat(result).isNotNull();
verify(...)...();
verifyStatic(StaticTokenUtils.class);
StaticTokenUtils.someStaticMethod();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment