Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unit test code which uses Java UUID?

I have a piece of code which is expected to populated one attribute of response object with Java UUID (UUID.randomUUID()).

How can I unit test this code from outside to check this behaviour? I don't know the UUID that would be generated inside it.

Sample code which needs to be tested:

// To test whether x attribute was set using an UUID
// instead of hardcode value in the response
class A {
  String x;
  String y;
}

// Method to test
public A doSomething() {
  // Does something
  A a = new A();
  a.setX( UUID.randomUUID());
  return a;
}
like image 558
Harish Avatar asked Mar 31 '16 10:03

Harish


People also ask

How does UUID work in Java?

A UUID represents a 128-bit value. It is used for for creating random file names, session id in web application, transaction id etc. There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs.

Is Java UUID really unique?

Java UUID ClassA UUID is a class that represents an immutable Universally Unique Identifier (UUID). A UUID represents a 128-bit long value that is unique to all practical purpose. It is used to identify information in the computer system.


2 Answers

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());
like image 60
RZet Avatar answered Oct 14 '22 17:10

RZet


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.

like image 38
Eran Boudjnah Avatar answered Oct 14 '22 18:10

Eran Boudjnah