jeudi 24 septembre 2015

how to mock methods of my superclass using Mockito

I am facing issues in mocking the data for the methods which are being called without the reference e.g getMethod(); don't know how will mocking framework know about it. Below is the code for which am facing issue am not able to set HttpRequest and URIInfo in my code.

Is it possible to bypass the method.

Class A {

private HttpServletRequest httpRequest;
private UriInfo uriInfo;

public HttpServletRequest getReq() {
    return httpRequest;
}
public void setReq(HttpServletRequest req) {
    this.httpRequest = req;
}


public UriInfo getUriInfo() {
    return uriInfo;
}
public void setUriInfo(UriInfo uriInfo) {
    this.uriInfo = uriInfo;
}}



class B extends A {

// some code

}

class C extends B {

protected Object executeCall(Object beIn) throws Exception{

prepareUpdateConfigurationRequest();
// some other methods.
return "";
}
private void prepareUpdateConfigurationRequest() {
implPutCustomerProductOrderIdProductConfigurationsImpl.setReq(getReq());
implPutCustomerProductOrderIdProductConfigurationsImpl.setUriInfo(getUriInfo());
}}

// Test class using Mockito Framework

@RunWith(MockitoJUnitRunner.class)
public class CTest {

@Mock
private A a = Mockito.mock(A.class);
@InjectMocks
private C c = new C();

private ImplBackEndInput implBackEndInput;

@Test
public void testExecuteCallObject() {



    implBackEndInput = new ImplBackEndInput();

    UriInfo uriInfo = Mockito.mock(UriInfo.class);

    Mockito.when(A.getUriInfo()).thenReturn(uriInfo);
    Mockito.when(A.getReq()).thenReturn(httpServletRequest);

    try {
        C.executeCall(implBackEndInput);
    } catch (Exception e) {
    }

}
}

Aucun commentaire:

Enregistrer un commentaire