Java mockito - 模拟接口 - 抛出 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19689061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:24:15  来源:igfitidea点击:

mockito - mocking an interface - throwing NullPointerException

javaandroidmockito

提问by Arun K

I am getting null pointer exception after mocking also. Please find my project structure.

我也在模拟后得到空指针异常。请找到我的项目结构。

    //this is the pet interface
    public interface Pet{
    }

    // An implementation of Pet
    public class Dog extends Pet{
        int id,
        int petName;

    }
    // This is the Service Interface
    public interface PetService {
        List<Pet> listPets();
    }

    // a client code using the PetService to list Pets
    public class App {
        PetService petService;

        public void listPets() {
             // TODO Auto-generated method stub
             List<Pet> listPets = petService.listPets();
             for (Pet pet : listPets) {
                System.out.println(pet);
             }
        }
    }

    // This is a unit test class using mockito
    public class AppTest extends TestCase {

        App app = new App();
        PetService petService = Mockito.mock(PetService.class);
        public void testListPets(){
            //List<Pet> listPets = app.listPets();
            Pet[] pet = new Dog[]{new Dog(1,"puppy")};
            List<Pet> list = Arrays.asList(pet);
            Mockito.when(petService.listPets()).thenReturn(list);
            app.listPets();
        }
   }

I am trying to use TDD here, Means I have the service interface written, But not the actual implementation. To test the listPets() method, I clearly knows that its using the service to get the list of pets. But my intention here to test the listPets() method of the App class, Therefore I am trying to mock the service interface.

我在这里尝试使用 TDD,意味着我已经编写了服务接口,但不是实际的实现。为了测试 listPets() 方法,我清楚地知道它使用该服务来获取宠物列表。但是我在这里打算测试 App 类的 listPets() 方法,因此我试图模拟服务接口。

The listPets() method of the App class using the service to get the pets. Therefore I am mocking that part using mockito.

使用服务获取宠物的 App 类的 listPets() 方法。因此,我正在使用 mockito 来嘲笑那部分。

    Mockito.when(petService.listPets()).thenReturn(list);

But when the unit test is running , perService.listPets() throwing NullPointerExceptionwhich I have mocked using the above Mockito.whencode. Could you please help me on this?

但是当单元测试运行时, perService.listPets() 抛出NullPointerException我已经使用上面的Mockito.when代码进行了模拟。你能帮我解决这个问题吗?

采纳答案by Chris

NullPointerException is because, in App, petService isn't instantiated before trying to use it. To inject the mock, in App, add this method:

NullPointerException 是因为,在 App 中,petService 在尝试使用之前没有被实例化。要注入模拟,请在 App 中添加此方法:

public void setPetService(PetService petService){
    this.petService = petService;
}

Then in your test, call:

然后在您的测试中,调用:

app.setPetService(petService);

before running app.listPets();

跑步前 app.listPets();

回答by Srinivas Pasupulate

You can also use @InjectMocksannotation, that way you wont need any getters and setters. Just make sure you add below in your test case after annotating your class,

您还可以使用@InjectMocks注释,这样您就不需要任何 getter 和 setter。只需确保在对类进行注释后在测试用例中添加以下内容,

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