Mockito框架抛出NullPointerException

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

比如下面的代码

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();
    }
}

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();
    }
}

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");
    }
}

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

Mockito框架抛出NullPointerException

分析上面为什么抛出NullPointerException

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

就比如下面的companies的变量是OX001

Mockito框架抛出NullPointerException

然后当真正执行到Company类里面的companyList()方法的时候,因为test1()方法里面有重新new了一个集合当成companyList()方法的参数,此时test1()方法里面又重新new的集合companyList变量的地址就会是OX003,

Mockito框架抛出NullPointerException
然后因为show()测试方法里面companies变量的地址和test1()方法里面的new的companyList变量的地址是不同的,所以show()方法里面的Mockito.when(company.companyList(companies)).thenReturn("hello")是执行不成功的,然后就会返回null,然后因为在test1()方法里面调用test2()方法,所以此时就会抛出空指针

怎么解决这个问题呢

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

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();
    }
}

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();
    }
}

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");
    }
}

此时结果如下所示,成功了

Mockito框架抛出NullPointerException

原文链接

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

上一篇:如何重建SYSAUX表空间上的对象


下一篇:最佳编程字体:M+