Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito断言结果NullPointerException_Java_Spring Boot_Mockito - Fatal编程技术网

Java Mockito断言结果NullPointerException

Java Mockito断言结果NullPointerException,java,spring-boot,mockito,Java,Spring Boot,Mockito,我正在编写一个简单的测试用例,但是在返回值中得到一个空指针异常。 虽然当我调试时,实际的模拟正在工作,并且我的主代码也被触发 我的测试用例 @Test public void testEmptyRequests() { List<MyPojo> list = new ArrayList(); String env = "env-stub"; when(myService.fetchRecords(env)).thenRetur

我正在编写一个简单的测试用例,但是在返回值中得到一个空指针异常。 虽然当我调试时,实际的模拟正在工作,并且我的主代码也被触发

我的测试用例

  @Test
  public void testEmptyRequests() {
     List<MyPojo> list = new ArrayList();
     String env = "env-stub";
     when(myService.fetchRecords(env)).thenReturn(list);
     Assert.assertEquals(Integer.valueOf(0), myController.process(env, 1));
 }
@测试
公共无效测试tyrequests(){
列表=新的ArrayList();
字符串env=“env stub”;
when(myService.fetchRecords(env)).thenReturn(list);
Assert.assertEquals(Integer.valueOf(0),myController.process(env,1));
}
我的方法

      @Async
      public Integer process(String environment, Integer id) {
      List<MyPojo> list = myService.fetchRecords(environment);
      if (list == null || list .isEmpty()) {
          log.info("[0] claims to process");
         return 0;
        }
            // Other logic here, Not relevant to this test case
     }
@Async
公共整数进程(字符串环境,整数id){
List=myService.fetchRecords(环境);
if(list==null | | list.isEmpty()){
log.info(“[0]请求处理”);
返回0;
}
//此处的其他逻辑与此测试用例无关
}

将测试方法更改为:

    @Test
    public void testEmptyRequests() {
        List<MyPojo> list = new ArrayList();
        String env = "env-stub";
        when(myService.fetchRecords(Mockito.anyString())).thenReturn(list);
        Assert.assertEquals(Integer.valueOf(0), myController.process(env, 1));
 }
@测试
公共无效测试tyrequests(){
列表=新的ArrayList();
字符串env=“env stub”;
when(myService.fetchRecords(Mockito.anyString())。然后return(list);
Assert.assertEquals(Integer.valueOf(0),myController.process(env,1));
}

您应该模拟代码中使用的方法。

将测试方法更改为:

    @Test
    public void testEmptyRequests() {
        List<MyPojo> list = new ArrayList();
        String env = "env-stub";
        when(myService.fetchRecords(Mockito.anyString())).thenReturn(list);
        Assert.assertEquals(Integer.valueOf(0), myController.process(env, 1));
 }
@测试
公共无效测试tyrequests(){
列表=新的ArrayList();
字符串env=“env stub”;
when(myService.fetchRecords(Mockito.anyString())。然后return(list);
Assert.assertEquals(Integer.valueOf(0),myController.process(env,1));
}

您应该模拟代码中使用的方法。

您正在为
when(myService.pullRequests(env))设置期望值。然后返回(list)
但是您的方法正在调用
myService.fetchRecords(环境)
您正在设置
的期望值,当(myService.pullRequests(env))。然后返回(list)但是您的方法正在调用
myService.fetchRecords(环境)
对不起,“when”块中不正确的方法名称是一个输入错误,我现在已经在问题中更正了它。我仍然有这个问题。谢谢你在测试中注入了mocked myService吗?是的,我注入了@mockbeansory,“when”块中不正确的方法名称是一个拼写错误,我现在已经在问题中更正了它。我仍然有这个问题。谢谢你在测试中注入了mocked myService吗?是的,我注入了@Mockbean