Reputation: 7969
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;
}
Upvotes: 14
Views: 36185
Reputation: 1278
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.
Upvotes: 7
Reputation: 106470
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
}
Upvotes: 3
Reputation: 16525
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.
Upvotes: 3
Reputation: 1074
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());
Upvotes: 13