Loading
 
What's Next: View All Tutorial →

How to mock private Method with and without parameter using JMockit with Deencapsulation

In this article, you will learn how to mock private Method using JMockit with Deencapsulation  User Java Class package com.mycompany.app; import java.util.ArrayList; import java.util.List; public class User { public String in...

Published Date: 29/04/2015  Last Modified Date: 29/04/2015    New Post



In this article, you will learn how to mock private Method using JMockit with Deencapsulation 

User Java Class

package com.mycompany.app;

import java.util.ArrayList;
import java.util.List;

public class User {

    public String invokeGetUserSkill() {
        Skill sk = new Skill();

        return sk.getUserSkill("Backend");
    }

    public String invokeUserUISkill() {
        Skill sk = new Skill();

        return sk.getUserSkill("UI");
    }
}

class Skill {

    public String getUserSkill(String type) {

        if ("Backend".equals(type)) {

            return getBackendSkill();

        }
        if ("UI".equals(type)) {

            return getUISkill("Jasmine");

        }
        return null;
    }

    private String getBackendSkill() {

        return "Java";
    }

    private String getUISkill(String result) {

        return result;
    }
}
 

Test Class:

 
package com.mycompany.app;

import mockit.Deencapsulation;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.integration.junit4.JMockit;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JMockit.class)
public class UserTest {

    @Test
    public void testMethodinClassUsingMockup() {

        new MockUp<Skill>() {
            @Mock
            private String getBackendSkill() {

                return null;
            }

            @Mock
            private String getUISkill(String result) {
                return null;
            }

        };
        User user = new User();
        Assert.assertEquals(user.invokeGetUserSkill(), null); //return null instead of Java, JMockit as we mocked
        Assert.assertEquals(user.invokeUserUISkill(), null);// return  null instead of Jasmine as we mocked
    }

    @Test
    public void testMethodinClassUsingExpectation(@Mocked final Skill skill) {

        new NonStrictExpectations() {
            {

                Deencapsulation.invoke(skill, "getBackendSkill");
                returns(null);

                Deencapsulation.invoke(skill, "getUISkill", anyString);
                returns(null);

            }
        };

        User user = new User();
        Assert.assertEquals(user.invokeGetUserSkill(), null);// return  null instead of Java, JMockit, as we mocked
        Assert.assertEquals(user.invokeUserUISkill(), null);// return  null instead of Jasmine as we mocked

    }
}


Mockup and Expectation are two different way to mock up the method. Here we use both the method. Please refere previous article for more details.


Expectation Way

Deencapsulation  -  Deencapsulation uses Java refecltion util to access the method. It has setField to access the class level field and invoke() method to access the private , protected method .


Using Deencapsulation, we can mock the static method also.

We need to use NonStrictExpectaion to use Deencapuslation. While returning the mocked object. the syntax is returns( mocked object)



Deencapsulation.invoke(skill, "getBackendSkill");


Deencapsulation.invoke(skill, "getUISkill", anyString);

 
Awaiting for Administrator approval





Tags: JMockit JUnit

← Back to list


Related Post




×