模拟对象抛出NPE

时间:2016-07-18 12:25:32

标签: spring mockito

我将为我的服务层创建mockito测试,我给出了一个下面这个层的一个例子

 @Service
public class LangServiceImpl implements LangService {
    @Autowired
    private LangDaoImpl langDao;

    public LangDaoImpl getLangDao() {
        return langDao;
    }

    public void setLangDao(LangDaoImpl langDao) {
        this.langDao = langDao;
    }

    ...
}

当我运行我的测试时,我得到了这个NPE

java.lang.NullPointerException
    at com.phonebook.service.LangServiceTest.getByIdTest(LangServiceTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我写了这个测试

 @RunWith(MockitoJUnitRunner.class)
    public class LangServiceTest {
        private static final Logger LOG = Logger.getLogger(AddressDao.class);
        private final String COUNTRY_NAME = "Ukraine";

        @InjectMocks
        LangServiceImpl langService = new LangServiceImpl();

        @Mock
        LangDaoImpl langDao;

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

        @Test
        public void getByIdTest(){
            try {
                when(langDao.findById(1).getLang()).thenReturn(COUNTRY_NAME);
                Assert.assertEquals(langService.findById(1).getLang(),COUNTRY_NAME);
            } catch (DataBaseException e) {
                LOG.error("Test not pass");
            }
        }
    }

所以我尝试使用这种方式创建模拟对象LangDaoImpl mock = mock(LangDaoImpl.class);,但结果相同。请帮忙解决问题

2 个答案:

答案 0 :(得分:1)

你的langDao.findById(1)返回null。 首先模拟它返回的值:

this.Dispatcher.BeginInvoke(new Action(() => {
            this.Run1.Text = e.Result.ToString();
        }));

另外,使用 @Mock LangDao 而不是 @Mock LangDaoImpl。

编辑:还有一件事:如果您使用MockitoJUnitRunner,则不必使用MockitoAnnotations.initMocks(...)。

答案 1 :(得分:0)

我可以假设这一行发生异常:

when(langDao.findById(1).getLang()).thenReturn(COUNTRY_NAME);

特别是在这个地方findById(1).getLang()

首先解决方案:

LangDaoImpl模拟创建上使用Answer#RETURNS_DEEP_STUBS

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
LangDaoImpl langDao;


解决方案第二:

将返回值更改为包含预期数据的实体。

when(langDao.findById(1)).thenReturn(new YourEntity(COUNTRY_NAME));