使用 JUnit 测试受保护的方法
2022-09-05 00:40:38
我正在测试一种方法,该方法是.在我的测试用例中,我习惯于访问该方法,但我不完全确定我是否以正确的方式执行此操作。protected
Reflection
测试方法:
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
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());
}
我的JUnit通过,但不确定它是否进入方法内部。