13

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;
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
Harish
  • 7,589
  • 10
  • 36
  • 47
  • 2
    You can still check the attribute has an value which is parseable UUID... – gusto2 Mar 31 '16 at 10:35
  • Did you try mocking? – seenukarthi Mar 31 '16 at 10:35
  • 2
    Create a `UuidGenerationStrategy` that you can replace in order to return a preprogrammed value. – chrylis -cautiouslyoptimistic- Mar 31 '16 at 10:38
  • In your test, do several calls of `doSomething()` and check if the resulting instances have different values in `x`. – Sebastian Mar 31 '16 at 10:39
  • 3
    You could also pass in a `Supplier` so that in the test you use a supplier that always returns the same UUID. – John B Mar 31 '16 at 10:39
  • You can try with PowerMockito to mock the static method, see: https://examples.javacodegeeks.com/core-java/mockito/mockito-mock-static-method-example/ – VinhNT Mar 31 '16 at 10:42
  • I am using Mockito.. If i have to go the Strategy/Supplier way, then I have to create a separate interface and two implementations one mock & other using UUID.. and pass this interface as constructor to the class and then use it.. This all looks too much just to test that piece? Is there a better way to handle this? – Harish Mar 31 '16 at 10:43
  • Finally went with the approach of mocking only by using the Strategy / Supplier approach. Thanks for your suggestions. – Harish Apr 01 '16 at 15:24

4 Answers4

11

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());
RZet
  • 914
  • 9
  • 11
  • I see this is also a viable approach. But as I just had only one such use-case in my project and I am currently on Mockito, instead of adding PowerMock dependency I went with the mock interface appraoch. – Harish Apr 01 '16 at 11:58
  • I don't think you can mock a static method with Mockito, unless you refactored your original code? Adding PowerMock dependency just for this case shouldn't impact your existing code as both frameworks can coexist in the same project. – RZet Apr 01 '16 at 12:21
  • True.. That's why I had created an interface like UUIDGenerator having generateUUID() method. In actual code implemented it using UUID.randomUUID() where as in test cases I just mocked that interface – Harish Apr 01 '16 at 15:23
  • 1
    Well, this is a compromise you need to make, either a single line of code and heavyweight testing or an additional interface with its implementation and lightweight testing. It's up to you to choose the best approach for your problem but sometimes you might end up with an excessive number of small interfaces. You might consider implementing a concrete class only and mock it in a similar way as an interface. – RZet Apr 01 '16 at 15:45
  • 4
    `@PrepareForTest({ UUID.class })` does not make sense, because it's final system class. Hence it is always loaded by bootstrap classloader. PowerMock can't modify it. But PowerMock can replace a call to the UUID class. A class that use `UUID` has to be added to '@PrepareForTest'. – Artur Zagretdinov Jun 30 '17 at 12:23
  • @RZet - Is there a way to run PowerMockito with Scalatest? – Manu Chadha Mar 07 '19 at 19:29
7

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.

Eran Boudjnah
  • 1,228
  • 20
  • 22
3

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.

ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
2

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
}
Makoto
  • 104,088
  • 27
  • 192
  • 230