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
.