Jmockit Mocking Example

The subject is mocking with Jmockit framework. Mocking class, mocking static method, final method examples are exist.

JMockit version : 1.38
Junit version : 4.12
Jdk = 1.7 or 1.8
github sourceCode is : github link


        <dependency>

            <groupId>org.jmockit</groupId>

            <artifactId>jmockit</artifactId>

            <version>1.38</version>

            <scope>test</scope>

        </dependency> 


Unit test test class is below.


package com.groupId.com.test.module.testmodule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mock;
import mockit.MockUp;
import mockit.Tested;
import mockit.Verifications;
import mockit.integration.junit4.JMockit;

@RunWith(JMockit.class)
public class JmockitV2 {
   
    @Tested
    FooClass fooClass;
   
    @Injectable
    FooService fooServiceMock;
   
    @Injectable
    StaticMethodClass staticMethodClassMock;
   
    @Injectable
    FinalClass finalClassMock;
   
    @Test
    public void fooClassTest(){
        new MockUp<InitClass>() {
            @Mock
            public void $init(){
               
            }
            @Mock
            public String methodMsg(){
                return "mock message";
            }
           
        };
       
       
        fooClass.constructorTest();
        fooClass.methodMsg();
    }

    @Test
    public void fooClassTest2(){
        new Expectations() {{
            fooServiceMock.sumService();
            result = "mock message from expectations";
        }};
       
        fooClass.doSth();
    }
   
    @Test
    public void staticMethodTest(){
       
        new MockUp<StaticMethodClass>() {
            @Mock
            public StaticMethodClass getInstance(){
                return staticMethodClassMock;
            }

            @Mock
            public String getMessage(){
                return "Mocked Message";
            }
        };
       
        fooClass.staticMethodCall();
    }
   
    @Test
    public void finalClassMethodTest(){
       
        new MockUp<FinalClass>() {
            @Mock
            public void $init(){
               
            }
           
            @Mock
            public String finalMethod(){
                return "final mock message";
            }
        };
        fooClass.finalClassMethodCall();
       
        new Verifications() {{
            assertTrue(finalClassMock.finalMethod().equals("final mock message"));
           
        }};
    }
}



This class is main test class. this contains other class have static, final methods vs.

package com.groupId.com.test.module.testmodule;

public class FooClass {
   
    private FooService service;
   
    public void doSth(){
        String result = service.sumService();
        System.out.println(result);
    }
   
    public void constructorTest(){
        InitClass initClass = new InitClass();
        initClass.doSth();
    }
   
    public void methodMsg(){
        InitClass initClass = new InitClass();
        String result = initClass.methodMsg();
        System.out.println(result);
    }
   
    public void staticMethodCall(){
        StaticMethodClass staticMethodClass = StaticMethodClass.getInstance();
        staticMethodClass.doSth();
        String result = staticMethodClass.returnSth();
        String returnResult = StaticMethodClass.getMessage();
        System.out.println(result + " " + returnResult);
    }
   
    public void finalClassMethodCall(){
        FinalClass finalClass = new FinalClass();
        String result = finalClass.finalMethod();
        System.out.println(result);
    }
}



Foo service is:

package com.groupId.com.test.module.testmodule;

public class FooService {

    public String sumService(){
        return "service method";
    }
} 

 


The class has static methods:


package com.groupId.com.test.module.testmodule;

public class StaticMethodClass {
   
   
    private static StaticMethodClass instance;
   
    public static StaticMethodClass getInstance(){
        if(instance == null){
            instance = new StaticMethodClass();
        }
        throw new RuntimeException("getinstance exception");
    }
   
    public static String getMessage(){
        return "real method message";
    }
   
    public void doSth(){
        throw new RuntimeException("doSth");
    }
   
    public String returnSth(){
        throw new RuntimeException("returnSth");
    }
}

 


The final classs has final method:


package com.groupId.com.test.module.testmodule;

public final class FinalClass {

    public FinalClass(){
        throw new RuntimeException("finalClass constructor exception");
    }
  
    public final String  finalMethod(){
        throw new RuntimeException("final method1 exception");
    }
}