How do I unit test code which uses Java UUID?

In relation to this existing question, it seems like the only way I was able to get the UUID to successfully mock out is if I added the class I wanted to test under @PrepareForTesting:

@PrepareForTesting({UUIDProcessor.class})
@RunWith(PowerMockitoRunner.class)
public class UUIDProcessorTest {
    // tests
}

Edit: having gained more experience with unit testing, I would opt for ThinkBonobo's answer. Create an interface, a fake implementation and a concrete implementation, like so:

public interface UuidProvider {
    UUID uuid();

    class Fake implements UuidProvider {
        @Override
        public UUID uuid() {
            return UUID.fromString("0000-00-00-00-000000");
        }
    }
}

public class RandomUuidProvider implements UuidProvider {
    @Override
    public UUID uuid() {
        return UUID.randomUUID();
    }
}

Inject UuidProvider.Fake in your tests, and RandomUuidProvider in your production code.

Or in Kotlin:

interface UuidProvider {
    fun uuid(): UUID

    class Fake : UuidProvider {
        override fun uuid() = UUID.fromString("0000-00-00-00-000000")
    }
}

class RandomUuidProvider : UuidProvider {
    override fun uuid() = UUID.randomUUID()
}

My old answer is below.


In addition to ThinkBonobo's response, another way it to create a getter method (optionally annotated with @VisibleForTesting) such as String getUUID() which can be overridden in a subclass you define in your test.


Powermock and static mocking is the way forward. You will need something like:

    ...
    import static org.junit.Assert.assertEquals;
    import static org.powermock.api.mockito.PowerMockito.mockStatic;
    ...

    @PrepareForTest({ UUID.class })
    @RunWith(PowerMockRunner.class)
    public class ATest
    {
    ...
      //at some point in your test case you need to create a static mock
      mockStatic(UUID.class);
      when(UUID.randomUUID()).thenReturn("your-UUID");
    ...
    }

Note the static mock can be implemented in a method annotated with @Before so it can be re-used in all test cases that require UUID in order to avoid code repetition.

Once the static mock is initialised, the value of UUID can be asserted somewhere in your test method as follows:

A a = doSomething();
assertEquals("your-UUID", a.getX());

When you need to mock, class/static methods become a real pain. What I ended up doing which will save you from using a mocking system is to use a thin wrapper class with a interface implementing the static methods.

In your code, instantiate/inject and use use the wrapper class instead of the static method. That way you can replace it with mocks.