Getting error when trying to mock class method to return value it is call real method

Below is the test class

`
@RunWith(Parameterized.class)
@SpringBootTest(classes = TelelogServiceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(“default”)
public class AtplInfotest {
private final static String ATPL_NEGATIVE_ID_NTG6 = “{“meta”:{“ATPL_ID”:”-1″}}”;

private final String serviceVersion;
private String ntgVersion;
private String snapVersion;
private String defaultAtplId;
private String apiVersion;
private String ecuSnapHeader;
private Class<?> clazz;
private MockMvc mockMvc;
@Mock
private HttpClient httpClient;
@Mock
private AppSettings appSettings;
@Mock
private AtplFileFactory atplFileFactory;

public AtplInfotest(Class<?> clazz, String serviceVersion, String ntgVersion,
                                        String snapVersion, String defaultAtplId,String apiVersion, String ecu) {
    this.clazz = clazz;
    this.serviceVersion = serviceVersion;
    this.defaultAtplId = defaultAtplId;
    this.ntgVersion = ntgVersion;
    this.snapVersion = snapVersion;
    this.apiVersion = apiVersion;
    this.ecuSnapHeader = "ecu=" + ecu;
}

@SuppressWarnings("rawtypes")
@Parameterized.Parameters
public static Collection data() {
    Object[][] data = new Object[][]{{WebServiceApiV2.class, "2", "NTG6-High-SOP", "SNAP/1.4", "000", Constants.API_VERSION_NTG6, "HU"}, {WebServiceApiV3.class, "3", "NTG7-High-SOP", "SNAP/1.5", "000", Constants.API_VERSION_NTG7, "HU"}, {WebServiceApiV3.class, "3", "NTG7-High-SOP", "SNAP/1.5", "000", Constants.API_VERSION_NTG7, "RSU"}};

    return Arrays.asList(data);
}

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

    when(appSettings.isEnableConfigurationService()).thenReturn(false);
    Mockito.when(appSettings.getGeo()).thenReturn("......");

    try {
        Constructor<?> constructor = clazz.getConstructor(WebService.class);
        Object instance = constructor.newInstance(new WebService(appSettings, httpClient, atplFileFactory));

        mockMvc = MockMvcBuilders.standaloneSetup(instance).build();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}


private static Response getResponse() {

………

    return response;
}

    @Test
public void requestWithNonDefaultAtplIdNegativeIdOnServer_shouldReturnDelete() throws Exception {

    when(httpClient.getResponse(".....")).thenReturn(getSacsResponse());

    when(atplFileFactory.getAtplFile(Constants.API_VERSION_NTG6, "NTG6", "unknown", null,false,getResponse())).thenReturn(new AtplFile(ATPL_NEGATIVE_ID_NTG6,Constants.ATPL_ID_KEY));

    String requestBody = atplInfoRequestBody(apiVersion, "abc");
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("...."));


    String responseBody = atplInfoResponseBody(apiVersion, "deleted");

    MvcResult result = mockMvc.perform(builder).andExpect(status().isOk()).andReturn();

    assertEquals(snapVersion, result.getResponse().getHeader("Server"));
    assertEquals(apiVersion, result.getResponse().getHeader("X-SSIV"));
    assertEquals("application/xml;charset=utf-8", result.getResponse().getContentType());
    assertEquals("0", result.getResponse().getHeader("X-Execution-Status"));
     assertEquals(responseBody, result.getResponse().getContentAsString());
}

private String atplInfoRequestBody(String apiVersion, String atplId) {
    return String.format(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><telelog x-ssiv=\"%s\"><request-atpl-info-request><atpl-id>%s</atpl-id></request-atpl-info-request></telelog>",
            apiVersion,
            atplId);
}

private String atplInfoResponseBody(String apiVersion, String atplAvailability) {
    return String.format(
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><telelog x-ssiv=\"%s\"><request-atpl-info-response><execution-status><status>OK</status><code>0</code><message>Successful execution.</message></execution-status><atpl-info>%s</atpl-info></request-atpl-info-response></telelog>",
            apiVersion,
            atplAvailability);
}

}
`

I Need to find why this test class is running properly , it is giving error whatever I try getAtplfile is not mock and not taking the return value I gave.

Leave a Comment