Hi,
I use PIT maven plugin 1.1.11, powermock 1.6.6, Junit 4.12. Below is the issue i am facing
I wrote a test class to test my target class. My target class has a method which use a Singleton (enum) object to do some operation and get some value. Now, my singleton class has got some some attributes which are loaded through spring ( via applicationContext.getBean()) in constructor. This bean which i am loading via spring has got some dependencies which has got JNDI configuration to for data sources, jms connections etc etc. Now, to write Junit for my class, i need an instance of this Singleton class in my target class, but, due to the JNDI configurations in the spring dependencies i am trying to load, i am forced to mock the Singleton class using mockito.
Here is what i did
Created a base test class, and added below in static block
static{
SimpleNamingContextBuilder builder = null;
try {
builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
builder.bind("wm/myWm", new WorkManagerTaskExecutor());
builder.bind("jms/ReqQueue", new MockDestination());
.......
builder.activate();
} catch (NamingException e) {
e.printStackTrace();
}
ApplicationContext context= new ClassPathXmlApplicationContext(new String[] {
"application-context.xml", "mytest-beans.xml" });
}
}
Below is my test class
@RunWith(PowerMockRunner.class)
@PrepareForTest({MySingleton.class})
public class MyTestClass extends BaseTestClass{
MySingletonClass mockSingleton=null;
TargetTestClass targetTestClass =null;
@Before
public void setUp() {
mockSingleton= PowerMockito.mock(MySingletonClass .class);
Whitebox.setInternalState(MySingletonClass .class, "INSTANCE", mockSingleton);
PowerMockito.when(mockSingleton,"getResult()",param,param,param,param).thenReturn(someObject);
}
@Test
public void testMethod_1(){
targetTestClass= new TargetTestClass()
Object result= targetTestClass.testMethod(params...);
Here goes assertions on result object.
}
My Junit just runs fine, satisfies all conditions, no issues. But now, i want to try PIT, so added the plugin, added target class and target tests (com.package.*) ,
run the command mvn org.pitest:pitest-maven:mutationCoverage and it fails.
Below is what i see in log as failure.
6:48:40 PM PIT >> INFO : MINION : 6:48:40 PM PIT >> WARNING : JUnit error for class class com.package.MyTestClass : com.package.MyTestClass
6:48:40 PM PIT >> INFO : MINION : 6:48:40 PM PIT >> WARNING : Not able to filter com.package.MyTestClass . Mutation may have prevented JUnit from constructing test
6:48:40 PM PIT >> FINE : MINION : FAIL Description [testClass=com.package.MyTestClass, name=testMethod_1(com.package.MyTestClass)] -> java.lang.ExceptionInInitializerError
I have another module for which PIT runs fine, so i dont have any doubts on configuration issues.