Strange case: using Powermock to mock out UUID.getRandom(). This works within the JUnit test class but when the class under test (a Filter, if that matters) calls UUID.getRandom, a unique UUID is produced.
Simple test case
@RunWith(PowerMockRunner.class)
@PrepareForTest({UUID.class})
public class MyTest {
private MyFilter filter;
@Before
public void setup() {
//The most convenient way to get a UUID, have also tried creating one manually
UUID uuid = UUID.randomUUID();
mockStatic(UUID.class);
PowerMockito.when(UUID.randomUUID()).thenReturn(uuid);
filter = new MyFilter();
}
@Test
public void testMyUUID() {
//This test works
assertEquals(UUID.randomUUID(), UUID.randomUUID());
}
@Test
public void testFilterUUID() {
//This test fails
assertEquals(UUID.randomUUID(), filter.getUUID());
}
}
Simple class being tested
public class MyFilter implements Filter {
public UUID getUUID() {
return UUID.randomUUID();
}
}
Pretty simple stuff, have done mocking like this before, just can't figure out why this case doesn't work.
Using Powermock version 1.5.
I believe UUID
falls under the "system classes" category, so you need to prepare for test the class calling UUID.randomUUID()
(related github issue)., thus changing to @PrepareForTest({MyFilter.class})
should fix things. The following works as expected with JUnit 4.4 & Powermock 1.5, as well as JUnit 4.12 and Powermock 1.7.3:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyFilter.class)
public class MyTest {
private MyFilter filter;
@Before
public void setup() {
//The most convenient way to get a UUID, have also tried creating one manually
UUID uuid = UUID.randomUUID();
mockStatic(UUID.class);
PowerMockito.when(UUID.randomUUID()).thenReturn(uuid);
filter = new MyFilter();
}
@Test
public void testMyUUID() {
//This test works
assertEquals(UUID.randomUUID(), UUID.randomUUID());
}
@Test
public void testFilterUUID() {
//This test fails
assertEquals(UUID.randomUUID(), filter.getUUID());
}
}