dimanche 29 juillet 2018

What is wrong with my unit testing?

I am new to unit testing.... can anyone help to correct my test, since my assertions are returning false and my test keeps failing, but I don't know if that is because I've written the test wrong or if the code being tested is wrong. Please help.

This is the code: public static String getTokenWithAppIdentity( String resource, String clientId, String clientSecret, String authority) throws IOException {

    String body = "resource=" + URLEncoder.encode(resource, java.nio.charset.StandardCharsets.UTF_8.toString())
            + "&client_id=" + URLEncoder.encode(clientId, java.nio.charset.StandardCharsets.UTF_8.toString())
            + "&client_secret=" + URLEncoder.encode(clientSecret, java.nio.charset.StandardCharsets.UTF_8.toString())
            + "&grant_type=client_credentials";

    return handleAuthRequest(body, authority);
}

and this is the test:

@Test
public void test_get_token_with_app_identity() throws IOException {

    PowerMockito.mockStatic(URLEncoder.class);
    Mockito.when(URLEncoder.encode(Mockito.anyString(), Mockito.anyString())).thenReturn("answer");

    PowerMockito.mockStatic(Authenticate.class);
    Mockito.when(Authenticate.handleAuthRequest(Mockito.anyString(), Mockito.anyString())).thenReturn("TOKEN");

    String result = Authenticate.getTokenWithAppIdentity("", "", "", "");

    Assert.assertNotNull(result);
    Assert.assertEquals("TOKEN", result);
}

Aucun commentaire:

Enregistrer un commentaire