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 !
JMockit will be the chosen option for its fixed-always-the-same structure. Mockito is more or less THE most known so that the community will be bigger. Having to call replay every time you want to use a mock is a clear no-go, so we'll put a minus one for EasyMock. Consistency/simplicity is also important for me.
First of all, let's talk about what JMockit is: a Java framework for mocking objects in tests (you can use it for both JUnit and TestNG ones). It uses Java's instrumentation APIs to modify the classes' bytecode during runtime in order to dynamically alter their behavior.
An expectation represents a set of invocations to a specific mocked method/constructor that is relevant for a given test. An expectation may cover multiple different invocations to the same method or constructor, but it doesn't have to cover all such invocations that occur during the execution of the test.
MockUp() Applies the mock methods defined in the concrete subclass to the class or interface specified through the type parameter.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With