当前位置:   article > 正文

Mockito框架抛出NullPointerException_mockito nullpointerexception

mockito nullpointerexception

文章首发于我的个人博客,到个人博客体验更佳阅读哦

https://www.itqiankun.com/article/1564905567

首先看一下Mockito框架是怎么抛出NullPointerException

比如下面的代码

在这里插入图片描述

Company类

package com.one.util;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public  class Company {
    private String name;

    public String companyList(List<Company> companies){
        return companies.get(0).getName();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Person类如下

package com.one.util;

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

import lombok.Data;

@Data
public class Person {

    private Company company;

    public  Person(Company company) {
        this.company = company;
    }

    public String test1() {
        List companyList = new ArrayList<Company>();
        companyList.add(company);
        return this.test2(company.companyList(companyList));
    }

    public String test2(String name) {
        return name.toString();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

PersonTest 测试类代码如下

package com.one.util;


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

import org.assertj.core.api.Assertions;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;


@RunWith(MockitoJUnitRunner.class)
public class PersonTest {
    @Mock
    private Company company;

    private Person person;

    @Before
    public void setUp(){
        person = new Person(company);
    }

    @Test
    public void show() {
        List<Company> companies = Lists.newArrayList();
        Mockito.when(company.companyList(companies)).thenReturn("hello");
        String s = person.test1();
        Assertions.assertThat(s).isEqualTo("hello");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

然后执行上面的测试方法show(),此时结果就会抛出空指针异常了,这是什么原因呢,我么往下看
在这里插入图片描述

分析上面为什么抛出NullPointerException

这是因为你的测试方法show()方法里面的Mockito.when(company.companyList(companies)).thenReturn(“hello”)里面的变量companies变量的地址和真正执行到Company类里面的companyList方法里面的companies参数变量的地址是不一样的,所以真正执行的时候,调用Mockito.when(company.companyList(companies)).thenReturn(“hello”)返回的就是null

就比如下面的companies的变量是OX001
在这里插入图片描述
然后当真正执行到Company类里面的companyList()方法的时候,因为test1()方法里面有重新new了一个集合当成companyList()方法的参数,此时test1()方法里面又重新new的集合companyList变量的地址就会是OX003,
在这里插入图片描述
然后因为show()测试方法里面companies变量的地址和test1()方法里面的new的companyList变量的地址是不同的,所以show()方法里面的Mockito.when(company.companyList(companies)).thenReturn(“hello”)是执行不成功的,然后就会返回null,然后因为在test1()方法里面调用test2()方法,所以此时就会抛出空指针

怎么解决这个问题呢

使用Mockito.any()就可以了
比如下面的代码

在这里插入图片描述

Company类

package com.one.util;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public  class Company {
    private String name;

    public String companyList(List<Company> companies){
        return companies.get(0).getName();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Person类如下

package com.one.util;

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

import lombok.Data;

@Data
public class Person {

    private Company company;

    public  Person(Company company) {
        this.company = company;
    }

    public String test1() {
        List companyList = new ArrayList<Company>();
        companyList.add(company);
        return this.test2(company.companyList(companyList));
    }

    public String test2(String name) {
        return name.toString();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

PersonTest 测试类代码如下,此时Mockito.when(company.companyList(any())).thenReturn(“hello”)里面使用的就是any()

package com.one.util;


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

import org.assertj.core.api.Assertions;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.any;

@RunWith(MockitoJUnitRunner.class)
public class PersonTest {
    @Mock
    private Company company;

    private Person person;

    @Before
    public void setUp(){
        person = new Person(company);
    }

    @Test
    public void show() {
        List<Company> companies = Lists.newArrayList();
        Mockito.when(company.companyList(any())).thenReturn("hello");
        String s = person.test1();
        Assertions.assertThat(s).isEqualTo("hello");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

此时结果如下所示,成功了
在这里插入图片描述

能看到这里的同学,就帮忙右上角点个赞吧,Thanks♪(・ω・)ノ

原文链接

大佬链接
https://www.itqiankun.com/article/1564905567

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/274465
推荐阅读
  

闽ICP备14008679号