jeudi 30 avril 2015

JUnit assertion to force a line to be executed

Is there any junit assertion, with which i can force a line to be executed?

For example:

doAnswer(new Answer<Void>() {
    @SuppressWarnings("unchecked")
    @Override
    public Void answer(final InvocationOnMock invocation) throws Throwable {
        Object[] arguments = invocation.getArguments();
        Map<String, String> fieldMapActual = (Map<String, String>) arguments[0];
        assertEquals(fieldMap, fieldMapActual);

        **assertFailIfThisLineIsNotExecuted();**

        return null;
    }
}).when(x).myMethod(xxx);

As i simulate the behaviour of myMethod, the method answer from the anonymous inner type will be executed at runtime of the myMethod (not at runtime of junit test), if myMethod will be called with the intended value/parameter. In order to assert that the method is called, i must additionally define a verify (otherwise my test would still run even if the method is not called).

verify(x).myMethod(xxx); 

If i had a chance to write sth like assertFailIfThisLineIsNotExecuted in the answer method, i would not have to define an extra verify. So again, Is there any junit assertion, with which i can force a line to be executed?

Aucun commentaire:

Enregistrer un commentaire