JUnit Mockito mock one method in another class Err

2019-06-08 17:54发布

问题:

I use Netbeans 8.1 , Junit and Mockito to write unit test for my project. Here is some pieces of my code

To be tested function:

public Map<String, String> getAllUsers() {

    if (allUsers == null) {
        if (session.checkACL2("DonateBookPrivilegeLevel") || session.checkACL2("ManageUserPrivilegeLevel")) {
            Iterator<User> it = userFc.findAll().iterator();
            System.out.println("PC::enum()");
            allUsers  = new HashMap<String, String>();
            while (it.hasNext()) {
                User item = it.next();
                allUsers.put(item.getName(), item.getUserId().toString());
            }
        }
    }
    return allUsers;
}

My Test class:

package com.controller;

import com.entities.User;
import com.jsfc.util.JsfUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.event.ActionEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;


@RunWith(MockitoJUnitRunner.class)
public class PersonalCenterControllerTest {

    public PersonalCenterControllerTest() {
    }

    @BeforeClass
    public static void setUpClass() {

    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
         MockitoAnnotations.initMocks(this);
    }

    @After
    public void tearDown() {
    }  

    /**
     * Test of getAllUsers method, of class PersonalCenterController.
     */
    @Test
    public void testGetAllUsers() {
        System.out.println("getAllUsers");

        PersonalCenterController pcController = new PersonalCenterController();

        SessionController session = Mockito.mock(SessionController.class);

        when(session.checkACL2(anyString())).thenReturn(true).thenReturn(true);

        Map<String,String> userMap = null;
        userMap = pcController.getAllUsers();

    } 
}

As you can see, I call session.checkACL2() which is defined in another class, I use Using Mockito to mock a class method inside another class to fix

SessionController session = Mockito.mock(SessionController.class);
doReturn(true).when(session).checkACL2((String) anyObject());

but it calls NullPointerException at if line.

回答1:

Are you actually passing mocked SessionController instance to your PersonalCenterController instance? If you try calling SessionController methods right after mocking does it return what you expected?

SessionController session = Mockito.mock(SessionController.class);
when(session.checkACL2(anyString())).thenReturn(true).thenReturn(true);
assertTrue(session.checkACL2("DonateBookPrivilegeLevel"));
assertTrue(session.checkACL2("ManageUserPrivilegeLevel");

As I see NullPointerException here could be from session instance either being null or returning null, that is then being converted to boolean. My first step would be to figure out which is it.



回答2:

Make sure you are running your JUnit with 'MockitoJUnitRunner.class'.

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest{

public void myMethodTest(){
   SessionController session = Mockito.mock(SessionController.class);
 Mockito.when(session).checkACL2(Mockito.anyString()).thenReturn(true).thenReturn(true);
  //Write logic to test your businesss logic inside class and assert the result.
}

}