How to Unit Test Classes Which Create New Objects
A simple method to write test cases for classes that use new keywords in their method with the help of Mockito and minimum code changes.
Join the DZone community and get the full member experience.
Join For FreeFirst of all, I will start with a disclaimer that I am a strong proponent of using the simple factory programming idiom and by extension of using the Factory Method Pattern, instead of creating objects inside classes. The factory idiom helps you to insulate your code to changes thereby adhering to the Open to Extension Close to modification principle of object-oriented programming.
You may also like: Unit Testing: The Good, Bad, and Ugly
Also, the idea is to write testable code, not to write a hack to test code.
Having said that I will showcase a simple method to write test cases for classes that use new keywords in their method with the help of Mockito and minimum code changes.
Documentation link for Mockito.
The first step is to import Mockito dependencies into your code.
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
Now let us see an example of how to test the class.
Let us assume the below is the class that we want to test.
xxxxxxxxxx
public class ClassToBeTested {
private ExternalClass object;
public void MethodToTest() {
object = new ExternalClass(arg1, arg2);
}
}
Below is the sample class of the object that is instantiated in ClassToBeTested.
xxxxxxxxxx
public class ExternalClass{
private Object obj1;
private Object obj2;
public ExternalClass(Object arg1, Object arg2){
this.obj1=arg1;
this.obj2=arg2;
}
}
The next step is to refactor the class to be tested and extract out the object instantiated into a separate method.
xxxxxxxxxx
public class ClassToBeTested {
private ExternalClass object;
public ExternalClass makeExternalClassObject(Object arg1, Object arg2){
return new ExternalClass(arg1, arg2);
}
public void MethodToTest() {
object = makeExternalClassObject(arg1, arg2);
}
}
One point to remember here is to avoid any logic in the make method so that we don’t have to write a unit test for this method.
Now let us write the test case for testing MethodToTest.
xxxxxxxxxx
MockitoJUnitRunner.class) (
public class ClassToBeTestedTest {
private ExternalClass externalClass;
public void testMethodToBeTested(){
ClassToBeTested classToBeTestedSpy = Mockito.spy(new ClassToBeTested());
Mockito.doReturn(externalClass).when(classToBeTestedSpy)
.makeExternalClassObject(arg1,arg2);
classToBeTestedSpy.MethodToTest();
/*Assertion Statement */
}
}
Done! With this trick, we can write unit tests even for classes that have object instantiations in their implementation. Given that implementing the factory pattern is not feasible or not worth the effort.
Thanks!
Follow me on twitter at @IamShivamMohan
Further Reading
How to Automate a Java Unit Test, Including Mocking and Assertions
Opinions expressed by DZone contributors are their own.
Comments