简体   繁体   English

mockito - 模拟界面 - 抛出NullPointerException

[英]mockito - mocking an interface - throwing NullPointerException

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. 我在这里尝试使用TDD,意味着我已经编写了服务接口,但不是实际的实现。 To test the listPets() method, I clearly knows that its using the service to get the list of pets. 为了测试listPets()方法,我清楚地知道它使用该服务来获取宠物列表。 But my intention here to test the listPets() method of the App class, Therefore I am trying to mock the service interface. 但我的意图是在这里测试App类的listPets()方法,因此我试图模拟服务接口。

The listPets() method of the App class using the service to get the pets. App类的listPets()方法使用该服务来获取宠物。 Therefore I am mocking that part using mockito. 因此,我使用mockito嘲笑那部分。

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

But when the unit test is running , perService.listPets() throwing NullPointerException which I have mocked using the above Mockito.when code. 但是当单元测试运行时,perService.listPets()抛出NullPointerException ,我已经使用上面的Mockito.when代码进行了模拟 Could you please help me on this? 你能帮帮我吗?

You can also use @InjectMocks annotation, that way you wont need any getters and setters. 您也可以使用@InjectMocks注释,这样您就不需要任何getter和setter。 Just make sure you add below in your test case after annotating your class, 只需确保在注释课程后在测试用例中添加以下内容,

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

NullPointerException is because, in App, petService isn't instantiated before trying to use it. NullPointerException是因为,在App中,petService在尝试使用之前未实例化。 To inject the mock, in App, add this method: 要在App中注入模拟,请添加以下方法:

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

Then in your test, call: 然后在你的测试中,致电:

app.setPetService(petService);

before running app.listPets(); 在运行app.listPets();之前app.listPets();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM