Looking now at a very simple example to test static methods:
1: public class MyStaticClass {
2: static boolean doSomethingCrazy() {
3: return true;
4: }
5: }
The test for this static methods could look as follows:
1: import static org.junit.Assert.assertFalse;
2: import org.junit.*;
3: import mockit.*;
4: public class StaticTest {
5: @Mocked
6: MyStaticClass myClass;
7: @Test
8: public void testMakeConnection(){
9: new NonStrictExpectations(){
10: // MyStaticClass is mocked here
11: {
12: MyStaticClass.doSomethingCrazy();
13: returns(false);
14: }
15: };
16: boolean wasItCrazy = MyStaticClass.doSomethingCrazy();
17: assertFalse(wasItCrazy);
18: new Verifications() {{
19: MyStaticClass.doSomethingCrazy(); times = 1;
20: }};
21: }
22: }
JMockit appears to be very powerful and elegant. I will publish more examples soon.
No comments:
Post a Comment