Search code examples
javajmockit

Testing private void method using jmockit


I have a private void method of a class .I want to test it using the Jmockit ,is there a Way ?

  private void logRetry(IOException ex) {
    if (log.isWarnEnabled()) {
        log.warn("I/O exception (" + ex.getClass().getName() + ") caught when processing request : "
                + ex.getMessage());
    }
    if (log.isDebugEnabled()) {
        log.debug(ex.getMessage(), ex);
    }
    log.info("Re-Trying the request on next available host");
}

Solution

  • Private method can be called by invoking Deencapsulation class in JMockit.

    invoke(java.lang.Object objectWithMethod,
     java.lang.String methodName, java.lang.Object... nonNullArgs)
    

    In your case, you can do similar like

    @Test
    public void testLogRetry() {
        YourClass testCalss = new YourClass();
        Deencapsulation.invoke(testClass, "logRetry", new IOException());
    }