Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMockit mock constructor

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 !

like image 1000
userx Avatar asked Aug 30 '13 20:08

userx


People also ask

What is the difference between JMockit and Mockito?

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.

What is JMockit in Java?

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.

What is expectation in JMockit?

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.

What is MockUp in Java?

MockUp() Applies the mock methods defined in the concrete subclass to the class or interface specified through the type parameter.


1 Answers

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.

like image 124
joescii Avatar answered Sep 22 '22 12:09

joescii