I am unit testing a class which has a complicated constructor ( with lots of parameters ). Constructor takes three arguments like :
public BehavioralDischargeCarePlan_Bus(Webform webForm,String dataEntryModel, String obsBatId) {
super(webForm, dataEntryModel, obsBatId);
.....
The constructor then calls a super constructor which gets even more complex. Using JMockit, how can I create an instance of the class and test a method without actually invoking the constructors ? I am new to JMockit, any help will be appreciated.
Thanks !
If I've understood you correctly, you want to test a class with a mocked constructor. This is not a good approach to testing because you aren't testing the production code in its purest form.
However, not everything goes according to the rules, does it? :) So if you insist, JMockIt will let you do this. You can mock out just the constructor and test the other methods. Mocking constructors is well-documented at the JMockIt project site.
Here is a quick demonstration you can try yourself:
Production code:
// src/main/java/pkg/SomeClass.java
public class SomeClass {
public static void main(String[] args) {
new SomeClass("a", 2);
}
public SomeClass(String a, Integer b) {
System.out.println("Production constructor called");
}
}
Mock code:
// src/test/java/pkg/SomeMock.java
import mockit.Mock;
import mockit.MockUp;
public class SomeMock extends MockUp<SomeClass> {
@Mock
public void $init(String a, Integer b) {
System.out.println("Mock constructor called");
}
}
Test code:
// srce/test/java/pkg/SomeTest.java
import org.junit.Test;
public class SomeTest {
@Test
public void test() {
new SomeMock();
new SomeClass("a", 2);
}
}
Running the production code will print Production constructor called
, but running it under test will print Mock constructor called
.