lundi 31 août 2020

Unable to mock a method which takes an interface

I have a service in my Spring Boot Project in which i have method which takes an interface.

interface IT {}
class AService {
 public String method(IT it) {}
}

I have two classes which implements that interface.

class AIT implements IT {}
class BIT implements IT {}

I am using this service method in some other service passing the AIT/BIT class object according to my need.

Now, I am writing the test cases for other service mocking the Service

public class OtherServiceTests {

    @MockBean
    private Service service;

  
    @Before
    public void setUp() {
    // none of these mocks working
        Mockito.when(service.method(Mockito.any()))
                .thenReturn("");
    Mockito.when(service.method(Mockito.any(IT.class)))
                .thenReturn("");
    Mockito.when(service.method(Mockito.any(BIT.class)))
                .thenReturn("");
    Mockito.when(service.method(Mockito.any(AIT.class)))
                .thenReturn("");
    // all returing to NullPointerException

    
        otherService = new OtherSerice();
    }

}
 

None of these mocks are working for this method only. Other mocks are working fine. It is returning NullPointerException which makes the tests fail.

I am new to testing using mockito. If anyone can guide me for this solution than this will be very helpful for me.

Aucun commentaire:

Enregistrer un commentaire