Loading
 
What's Next: View All Tutorial →

How to verify the mocked method is called n times using JMockit with MockUp and NonStrictExpectations

In this article, you will learn how to verify the mocked method is called n times using JMockit with MockUp and NonStrictExpectations User Class package com.mycompany.app; import java.util.ArrayList; import java.util.List; public class U...

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



In this article, you will learn how to verify the mocked method is called n times using JMockit with MockUp and NonStrictExpectations

User Class

package com.mycompany.app;

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

public class User {

    public List getSkill(String type) {

        Skill skill = new Skill();
        if ("Both".equals(type)) {

            List lst = skill.getBackendSkill();
            lst.add(skill.getUISkills());
            return lst;
        }
        return null;
    }

}

class Skill {

    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

 
package com.mycompany.app;

import java.util.ArrayList;
import java.util.List;
import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.Verifications;
import mockit.VerificationsInOrder;
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(invocations = 1)
//            @Mock(maxInvocations = 1)
//            @Mock(minInvocations = 1)
            public List getBackendSkill() {

                return new ArrayList();
            }

            @Mock(invocations = 1)
            public List getUISkills() {

                return new ArrayList();
            }
        };
        User user = new User();
        Assert.assertEquals(user.getSkill("Both").toString(), "[[]]"); //return [[]] instead of Java, JMockit, Extjs and jasmine as we mocked

    }

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

        new NonStrictExpectations() {
            {
                skill.getBackendSkill();
                result = new ArrayList();

                skill.getUISkills();
                result = new ArrayList();
            }
        };

        User user = new User();
        Assert.assertEquals(user.getSkill("Both").toString(), "[[]]");// return  instead of Java, JMockit, Extjs and jasmine as we mocked

        new Verifications() {
            {
                skill.getBackendSkill();
                times = 1;
//                maxTimes =1;
//                minTimes = 1;

                skill.getUISkills();
                times = 1;
            }
        };
        user.getSkill("Both");
        new VerificationsInOrder() {
            {
                skill.getBackendSkill();
                skill.getUISkills();
            }
        };

    }
}


Here using two ways we mock up the Skill class method.  Both are different approach in JMockit to mock the method

1. MockUp Method    

@Mock(invocations = 1)  - to check the mocked method is call only one time
@Mock(maxInvocations = 1)  - to check the mocked method is call more than one time
@Mock(minInvocations = 1)  - to check the mocked method is call atleast one time

Example:
            @Mock(invocations = 1)
            public List getUISkills() {

                return new ArrayList();
            }

2. NonStrictExpectation

 new Verifications() {   -  is used to check how many times the mocked method is called. It should be called after the asserts statement or the Main function call, here user.getSkill("Both");
       
new VerificationsInOrder() {   -  is used to check the mocked method are calling in the defined sequential order


The Verfications() and VerificationsInOrder() should be called when we use the new NonStrictExpectations() { style mock up

------------------------------------
Note:
The Mockup and the Exceptation are two different way of mock up. For more information Please refere the previous article
 
Awaiting for Administrator approval





Tags: JMockit JUnit

← Back to list


Related Post




×