mercredi 29 avril 2015

how jMockIt StrictExpectations work

I am relatively new with the jMockIt framework which I started using a few months ago on a personal project with a view of adapting it in one of our enterprise projects. So far it looks promising and it looks like a good alternative for our Mockito + Powermock combination that we have in place at the moment. This statement should not be read as I am not advocating for simplicity but not being obliged to compromise your design just for the sake of making things easier to test it is definitely very appealing.

In my view one other appealing thing was the availability of the Strict Expectations. The way I understood this concept was that if I have the class under test using multiple mocked collaborators then not setting the exact interactions with those mocked collaborators will cause the test to fail. In reality this does not seem to happen. It seems to me that in order for a mocked collaborator to have its interactions monitored you need to have at least one expectation set against that mocked collaborator.

When I realized this I felt a bit disappointed as it can easily lead to untested code when adding new collaborators. Hope my approach is not the right one and someone on here will be able to put me on the right direction. I put together a short test case to make it easier to reproduce what I am saying.

package my.testing.pkg;

import mockit.Mocked;
import mockit.StrictExpectations;
import mockit.Verifications;
import mockit.integration.junit4.JMockit;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(JMockit.class)
public class LoggerTest {
    class ClassUnderTest {
        private final Logger logger = Logger.getLogger(ClassUnderTest.class);
        String getString() {
            logger.debug("about to return a string");
            return "xyz";
        }
    }

    @Mocked
    private Logger logger;

    @Test
    public void shouldReturnTheRightString() throws Exception {
        new StrictExpectations() {{
        }};
        assertEquals("xyz", new ClassUnderTest().getString());
        new Verifications() {{
            logger.debug("about to return a string");
        }};
    }
}

The above unit test passes. It also passes if you comment out the whole verification block or if you do not have a verification block and move the logger interaction in the expectations block. What I expected was that if I have a mocked logger and there were interactions with that logger then the missing of either a strict expectation or a verification entry related to that logger will cause my test to fail.

Thank you in advance for any clarification.

Aucun commentaire:

Enregistrer un commentaire