mock(UUID.class) returns an actual UUID instance and not a mock.

5,099 views
Skip to first unread message

Marcel Overdijk

unread,
Nov 2, 2011, 5:46:24 PM11/2/11
to PowerMock
http://code.google.com/p/powermock/issues/detail?id=354

UUID uuid = org.powermock.api.mockito.PowerMockito.mock(UUID.class)
returns an actual UUID instance and not a mock.

I have this class:

public class SecurityService {
public String generatePerishableToken() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}

and the test class looks like:


@RunWith(PowerMockRunner.class)
@PrepareForTest({ SecurityService.class })
public class SecurityServiceTest {

private SecurityService service;
private UUID uuid;

@Before
public void setUp() {
service = new SecurityService();
uuid = mock(UUID.class);
}

@Test
public void testGeneratePerishableToken() {
// given
mockStatic(UUID.class);
given(UUID.randomUUID()).willReturn(uuid);
given(uuid.toString()).willReturn("my-perishable-token");

// when
String actual = service.generatePerishableToken();

// then
assertEquals("myperishabletoken", actual);
// TODO verify
}
}

Johan Haleby

unread,
Nov 3, 2011, 3:10:08 AM11/3/11
to powe...@googlegroups.com
It's expected behavior that mock(UUID.class) doesn't return a mock. This is "impossible" since UUID is a final system class. What happens in these cases is that the class is instantiated without using the constructor and PowerMock does its best to fill all state of the class with default values. (Have a look at the source code for details). 

I'll try to reproduce this when I find time.

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Marcel Overdijk

unread,
Nov 3, 2011, 4:04:59 PM11/3/11
to PowerMock
Thanks for your reply Johan.

But I don't understand yet what it exactly means.

As you state mock(UUID.class); will return an actual UUID class with
default values. I can actually see this yes.

But then there is no way to use:

mockStatic(UUID.class);
given(UUID.randomUUID()).willReturn(uuid);

as this will cause a NotAMockException.

So I wonder if this can be tested at or not, what is the use of
mock(UUID.class); even when getting back default values?




On Nov 3, 8:10 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> It's expected behavior that mock(UUID.class) doesn't return a mock. This is
> "impossible" since UUID is a final system class. What happens in these
> cases is that the class is instantiated without using the constructor and
> PowerMock does its best to fill all state of the class with default values.
> (Have a look at the source code for details).
>
> I'll try to reproduce this when I find time.
>
> /Johan
>
> On Wed, Nov 2, 2011 at 10:46 PM, Marcel Overdijk
> <marceloverd...@gmail.com>wrote:

Johan Haleby

unread,
Nov 3, 2011, 4:51:55 PM11/3/11
to powe...@googlegroups.com
I'm not sure why this doesn't work for UUID.class when it works for URL (see http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/mockito/junit4/src/test/java/samples/powermockito/junit4/system/SystemClassUserTest.java). I've added a work-around to trunk. Please see "mockingUUIDWorks" in that class.

/Johan

Marcel Overdijk

unread,
Nov 3, 2011, 5:06:24 PM11/3/11
to PowerMock
Hi Johan,

Is the workaround in internal (unreleased) powermock code or in the
tests?
I tried the same as in the test class you mentioned but I still get
the NotAMockException.

On Nov 3, 9:51 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> I'm not sure why this doesn't work for UUID.class when it works for URL
> (seehttp://code.google.com/p/powermock/source/browse/trunk/modules/module...).
> I've added a work-around to trunk. Please see "mockingUUIDWorks" in that
> class.
>
> /Johan
>

Johan Haleby

unread,
Nov 3, 2011, 5:20:19 PM11/3/11
to powe...@googlegroups.com
The code is not released yet since I just fixed it. In order to get the
test to run you need to build PowerMock from the source code.

/Johan

Marcel Overdijk

unread,
Nov 3, 2011, 5:21:48 PM11/3/11
to PowerMock
OK clear,
I was not sure if you had a workaround in the test or in actual lib.

Thanks again, Marcel

Johan Haleby

unread,
Nov 4, 2011, 3:17:48 AM11/4/11
to powe...@googlegroups.com
No problem. Your example seems very easy but the it's quite complex under the hood to actually mock it.

/Johan
Reply all
Reply to author
Forward
0 new messages