Search code examples
javajunit4easymockparameterized

EasyMock and parameterized tests (JUnit parameterized)


I would like to use @Mock on a class inside parameterized test class. But for some reasons mockClassB is NULL. My code similar to

@RunWith(Parameterized.class)
public class ClassATest extends EasyMockSupport {

    private String uniqueIdentifier;
    private String value;

    @Mock
    private ClassB mockClassB;

    public ClassATest(String uniqueIdentified, String value) {
        this.uniqueIdentifier = uniqueIdentified;
        this.value = value;
    }

    ...

    @Parameterized.Parameters(name = "{index}: id = {0}; value = {1}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {"1", "val1"},
                {"2", "val2"}});
    }

    @Test
    public void testMethod() {
        ...
        expect(mockClassB.someMethod(uniqueIdentifier)).andReturn(value);   // mockClassB is NULL
        replayAll();
        ....
    }
}

Is it possible to create a mocked object inside a parameterized class?


Solution

  • EasyMock needs a rule or a runner to instantiate the annotated mocks. Since you are already using a runner, your only option is a rule. The following will work.

    @RunWith(Parameterized.class)
    public class ClassATest extends EasyMockSupport {
    
        @Rule
        public EasyMockRule rule = new EasyMockRule(this);
    
        private String uniqueIdentifier;
        private String value;
    
        @Mock
        private ClassB mockClassB;
    
        public ClassATest(String uniqueIdentified, String value) {
            this.uniqueIdentifier = uniqueIdentified;
            this.value = value;
        }
    
        @Parameterized.Parameters(name = "{index}: id = {0}; value = {1}")
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][]{
                    {"1", "val1"},
                    {"2", "val2"}});
        }
    
        @Test
        public void testMethod() {
            expect(mockClassB.someMethod(uniqueIdentifier)).andReturn(value);   // mockClassB is NULL
            replayAll();
        }
    }
    

    The alternative is to directly call injectMocks which is available on EasyMockSupport.

    @RunWith(Parameterized.class)
    public class ClassATest extends EasyMockSupport {
    
        private String uniqueIdentifier;
        private String value;
    
        @Mock
        private ClassB mockClassB;
    
        public ClassATest(String uniqueIdentified, String value) {
            this.uniqueIdentifier = uniqueIdentified;
            this.value = value;
        }
    
        @Before
        public void before() {
            injectMocks(this);
        }
    
        @Parameterized.Parameters(name = "{index}: id = {0}; value = {1}")
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][]{
                    {"1", "val1"},
                    {"2", "val2"}});
        }
    
        @Test
        public void testMethod() {
            expect(mockClassB.someMethod(uniqueIdentifier)).andReturn(value);   // mockClassB is NULL
            replayAll();
        }
    }