vendredi 29 mai 2015

JMockit: Overriding @Mocked class

I have an internal StreamGobbler class that has 7 methods in it. I'm looking for a quick way to mock all the methods by default, but override one method (e.g. Partial Mocking). It seems like I need to use the @Mocked annotation in combination with MockUp to partially mock the getOutput method.

This is similar to the question JMockit: @Mocke and MockUp combination in the same test, but I can't get away with just looking at method counts.

If I have a test setup like this:

@Test
public void execute(@Mocked StreamGobbler sg)
{
    new MockUp<StreamGobbler>()
    {
        String type = null;

        @Mock
        void $init(String type)
        {
            this.type = type;
        }

        @Mock
        String getOutput()
        {
            if ("OUTPUT".equals(type))
            {
                return "test output";
            }
            else 
            {
                return "";
            }
        }
     }
 }

I get this error java.lang.IllegalArgumentException: Class already mocked

If I try to add the @Override annotation in the MockUp, it doesn't help (and Eclipse complains about it)

What is the best way to handle this? Use a static class outside this test method?

Using JMockit 1.17

Aucun commentaire:

Enregistrer un commentaire