uttam
uttam

Reputation: 455

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");
}

Upvotes: 1

Views: 1510

Answers (2)

Andrew Liu
Andrew Liu

Reputation: 351

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());
}

Upvotes: 2

Muhammad Suleman
Muhammad Suleman

Reputation: 2922

In my opinion, Every private method must use somewhere in public method in same class. So test that public method it will automatically test that private method.

Upvotes: 5

Related Questions