我使用 Netbeans 8.1、Junit 和 Mockito 为我的项目编写单元测试。这是我的一些代码片段
待测试功能:
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;
}
我的测试类:
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();
}
}
如您所见,我调用在另一个类中定义的session.checkACL2(),我使用Using Mockito模拟另一个类中的类方法来修复
SessionController session = Mockito.mock(SessionController.class);
doReturn(true).when(session).checkACL2((String) anyObject());
但它在 if 线调用NullPointerException
。
您实际上是将模拟的SessionController
实例传递给PersonalCenterController
实例吗?如果您在模拟后立即尝试调用SessionController
方法,它会返回您期望的内容吗?
SessionController session = Mockito.mock(SessionController.class);
when(session.checkACL2(anyString())).thenReturn(true).thenReturn(true);
assertTrue(session.checkACL2("DonateBookPrivilegeLevel"));
assertTrue(session.checkACL2("ManageUserPrivilegeLevel");
正如我所看到的NullPointerException
这里可能来自session
实例,要么被null
要么返回null
,然后被转换为boolean
。我的第一步是弄清楚是哪个。
确保使用"MockitoJUnitRunner.class"运行JUnit。
@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.
}
}