13

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 !

userx
  • 1,083
  • 5
  • 18
  • 36

1 Answers1

21

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.

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
joescii
  • 6,495
  • 1
  • 27
  • 32
  • `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.` I completely disagree. Unit testing (white box testing - which is what JMockit is used for) in its purest form is to test individual units. If you are testing a particular unit that is not a complex constructor, then allowing it to execute is black box testing which is incredibly bad to do when you're trying to do unit testing. – searchengine27 Sep 08 '15 at 19:15
  • but how do you set the object's fields when mocking your constructor (inside $init() method)? – supertonsky Sep 10 '15 at 08:44
  • It works well with the mockup constructor. When I new instance of a class, how can I return another existing object? some one said about "magic 'it' attribute" http://stackoverflow.com/questions/4563584/how-to-mock-the-default-constructor-of-the-date-class-with-jmockit – Marco Dinh Nov 01 '15 at 08:48
  • 6
    The link to the JMockit project page is down. – michaelbahr Mar 01 '16 at 09:27