Loading
 
What's Next: View All Tutorial →

How to mock public method using MockUp and Exceptation class in JMockit

In this article, you will learn How to mock public method using MockUp and Exceptation class in JMockit User Class   package com.mycompany.app; import java.util.ArrayList; import java.util.List; public class User { public Li...

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



In this article, you will learn How to mock public method using MockUp and Exceptation class in JMockit

User Class

 
package com.mycompany.app;

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

public class User {

    public List getSkill(String type) {

        if ("Backend".equals(type)) {
            return getBackendSkill();
        } else if ("UI".equals(type)) {
            return getUISkills();
        }
        return null;
    }

    public List getUISkills() {

        List lst = new ArrayList();

        lst.add("Extjs");
        lst.add("Jasmine");
        return lst;
    }

    public List getBackendSkill() {
        List lst = new ArrayList();
        lst.add("Java");
        lst.add("JMockit");

        return lst;
    }
}

Test Class

We are going to mock the getSkill(String type) method
package com.mycompany.app;

import java.util.List;
import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
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<User>() {
            @Mock
            public List getSkill(String type) {

                return null;
            }
        };
        User user = new User();
        Assert.assertEquals(user.getSkill("Backend"), null); //return null instead of Java and JMockit as we mocked

    }

    @Test
    public void testMethodinClassUsingExpectation(@Mocked final User user) {

        new Expectations() {
            {
                user.getSkill("UI");
                result = null;
            }
        };
        Assert.assertEquals(user.getSkill("UI"), null); //return null instead of Jasmine and Extjs as we mocked
    }
}

We can mock the method in Jmockit in two ways

1.  @MockUp


MockUp is the inline method,

Snytax:

new MockUp<User>() {           //User - is the class name , We can give any class to be mocked
            @Mock
            public List getSkill(String type) {    //The actual method declaration should be give to mock the method

                return null;                              // When the method is mocked this body function will be executed and returned
            }
        };

 

2.  Expectations


Syntax:

 new Expectations() {
            {
                <mocked_object>.<method_name>(<param_value>);
                result = <the mocked object>;
            }
        };

<mocked_object> should be defined as @Mocked

The @Mocked can be given as a parameter of the test method.
Exmaple : public void testMethodinClassUsingExpectation(@Mocked final User user)


or At the class level variable. If we declared at class level variable other test method can also be used.

Example:
@Mocked
    User user;  //Class level variable

    @Test
    public void testMethodinClassUsingExpectation() {

        new Expectations() {
            {
                user.getSkill("UI");
                result = null;
            }
        };
        Assert.assertEquals(user.getSkill("UI"), null); //return null instead of Jasmine and Extjs as we mocked
    }




 
Awaiting for Administrator approval





Tags: JMockit JUnit

← Back to list


Related Post




×