2
private static String getToken(HttpClient clientInstance) throws badcredentailsexception{
try{
    // some process here throws IOException
    }
catch(IOexception e){
    throw new badcredentailsexception(message, e)
   }
}

Now I need to write Junit test for the above method, My Junit code for above function is below

@Test(expected = badcredentailsexception.class)
public void testGetTokenForExceptions() throws ClientProtocolException, IOException, NoSuchMethodException, SecurityException, IllegalAccessException, 
                        IllegalArgumentException, InvocationTargetException {

  Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class))).thenThrow(IOException.class);
 // mocked mockHttpClient to throw IOException

    final Method method = Client.class.getDeclaredMethod("getToken", HttpClient.class);
    method.setAccessible(true);
    Object actual = method.invoke(null, mockHttpClient);
    }

But this test is not being passed, any improvements??

Can we check the exception thrown by private method from junit ??

harish chava
  • 252
  • 2
  • 19
  • 1.explain what do you meant by test is not being passed. are you getting any exception? 2. I dont think you can test private methods using Mockito. You should use Powermockito – pvpkiran Dec 26 '17 at 13:57
  • The test method invokes `getToken(HttpClient)` but your method doesn't have the `HttpClient` parameter. Is this due to copy/paste or do you have two `getToken`methods? – Roland Weisleder Dec 26 '17 at 17:34
  • There needs to be a argument for the function, edited now @RolandWeisleder – harish chava Dec 27 '17 at 04:52
  • @pvpkiran I am expecting badcredentailsexception, But I got getting Invocation target exception from method.invoke statement along with bad credentials exception in Junit trace – harish chava Dec 27 '17 at 05:08
  • Possible duplicate of [Testing Private method using mockito](https://stackoverflow.com/questions/8799439/testing-private-method-using-mockito) – Stefan Birkner Jan 12 '18 at 09:55

2 Answers2

2

First of all, it is an antipattern to test a private method. It is not part of your API. See the already linked question: Testing Private method using mockito

To answer your question: When invoking a method via Reflection and the invoked method throws an Exception, the Reflection API wraps the Exception into an InvocationTargetException. So you could catch the InvocationTargetException and inspect the cause.

@Test
public void testGetTokenForExceptions() throws Exception {
    HttpClient mockHttpClient = mock(HttpClient.class);
    when(mockHttpClient.execute(any(HttpPost.class))).thenThrow(IOException.class);

    Method method = Client.class.getDeclaredMethod("getToken", HttpClient.class);
    method.setAccessible(true);

    try {
        method.invoke(null, mockHttpClient);
        fail("should have thrown an exception");
    } catch (InvocationTargetException e) {
        assertThat(e.getCause(), instanceOf(BadCredentialsException.class));
    }
}
Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59
0

You couldn't test private methods with JUnit or even with Mockito framework. You could find more details in this question: Testing Private method using mockito

If you really need to test this private method, you should use PowerMock framework.