Reputation: 45
I am writing a testcase which includes to mock a static method. Though not able to achieve the desired result.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
public class MockSubClassTest {
@Test
public void mockSuperClassTest(@Mocked final SuperClass superClass){
new MockUp<Util>() {
@Mock
public String getAppName(){
return "FXI";
}
};
SubClass subClass = new SubClass("data");
assertEquals("data", subClass.getData());
assertEquals("FXI", subClass.getApp());
}
}
public class SubClass extends SuperClass {
String data;
public SubClass(String string) {
super(string);
data = string;
}
public String getData() {
return data;
}
}
public class SuperClass {
public final static String app = Util.getAppName();
public SuperClass(String data) {
throw new IllegalArgumentException();
}
public String getApp(){
return app;
}
}
public class Util {
public static TestObject getObject() {
// TODO Auto-generated method stub
return null;
}
public static String getAppName() {
// TODO Auto-generated method stub
return null;
}
}
But while asserting for getAppName, it is failing. In the above code, I am able to mock super class constructor but unable to mock Util.getAppName().
I am new to JMockit, hence would appreciate help.
Upvotes: 1
Views: 2248
Reputation: 328568
I believe the problem is that SuperClass
is @Mocked
so the MockUp<Util>
is not applied.
This works as expected (changes commented out):
public void mockSuperClassTest(/*@Mocked final SuperClass superClass*/) {
//same code here
}
class SuperClass {
public final static String app = Util.getAppName();
public SuperClass(String data) {
//throw new IllegalArgumentException();
}
public String getApp() {
return app;
}
}
Upvotes: 2