mercredi 3 février 2016

Testing protected method with JUnit

I am testing a method which is protected. In my test case I've used Reflection to access the method but I am not exactly sure whether I am doing it in a right way.

Method to be tested:

protected void checkORCondition( Map<String, Message> messagesMap ) throws EISClientException
{
    Message message = containsAMessageCode(getMessageCodes(), messagesMap);
    if(message!=null)
    {
        throw new EISClientException("One of the specified message code matched returned errors." + 
                message.getMessageCode() + ": " + message.getMessageType() + ": " + message.getMessageText());

    }
}

JUnit test case:

@Test
public void testcheckORCondition() throws Exception {
    Class clazz = MessageToExceptionPostProcessFilter.class;
    Object object = clazz.newInstance();

    Method method = clazz.getDeclaredMethod("checkORCondition", new Class[]{Map.class});
    method.setAccessible(true);

    String string = new String();
    string = "testing";

    Message message = new Message();
    message.setMessageCode("200");

    Map<String, Message> map = new HashMap<String, Message>();
    map.put(string, message);

    assertEquals("testing", string);
    assertEquals("200", message.getMessageCode());  
}

My JUnit passes but not sure if it going inside the method.

Any help would be appreciated.





Aucun commentaire:

Enregistrer un commentaire