According to the example in the Jmockit tutorial this code should do the trick:
@BeforeClass
public static void setUpClass() {
new MockUp<UtilJndi>(){
@Mock
public static String getDirectoryFromContext(Property jndiName) // line 66
throws DirectoryNotFoundException {
return "noDirectory";
}
};
}
But it shows:
myclass.java:[66,29] error: Illegal static declaration
How can I resolve this?
I will add another workaround wich works for me:
I create my mocked class extending MockUp:
public static class MockUtilJndi extends MockUp<UtilJndi> {
public MockUtilJndi() {
super();
}
@Mock
public static String getDirectoryFromContext(Property jndiName)
throws DirectoryNotFoundException {
return "noDirectory";
}
}
If you notice I call the super() inside my constructor. Cause according to documentation if you call MockUp constructor it will change the implementation in the target class.. so once you have this in your mocked class constructor you just need to create your class inside the @BeforeClass annotated method:
@BeforeClass
public static void setUpClass() {
new MockUtilJndi();
}