Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow EasyMockSupport to be used through delegation with EasyMockRule or EasyMockRunner #258

Open
fbastien opened this issue May 8, 2020 · 1 comment

Comments

@fbastien
Copy link

fbastien commented May 8, 2020

EasyMockSupport and EasyMockRule/EasyMockRunner are great features that work well together when EasyMockSupport is used through inheritence.
However when using EasyMockSupport through delegation, it's currently (almost) impossible to make EasyMockRule aware of it, so mocks created with @Mock aren't taken into account by EasyMockSupport.

Here is a code snippet demonstrating the issue:

import org.easymock.*;
import org.junit.*;

// Class under test
public class MyClass {
    private Collaborator collaborator;
    public MyClass() {
        collaborator = new Collaborator();
    }

    // Method under test
    public int myMethod() {
        return collaborator.mockedMethod() + 1;
    }
}

// Class to be mocked
public class Collaborator {
    public int mockedMethod() {
        return 1;
    }
}

// Test case
public class MyClassTest { // May extend another class
    private EasyMockSupport easyMockSupport = new EasyMockSupport();
    @Rule public EasyMockRule easyMockRule = new EasyMockRule(this);

    @TestSubject private MyClass testSubject = new MyClass();
    @Mock private Collaborator collaboratorMock;

    @Test public void testMyMethod() {
        EasyMock.expect(collaboratorMock.mockedMethod()).andReturn(2);
        easyMockSupport.replayAll();

        int result = testSubject.myMethod();
        Assert.assertEquals("Should return 2+1 when successfully mocked", 3, result);
        // throws java.lang.AssertionError: expected: <3> but was: <1>
    }
}

This is a feature request to make the API provide a way to make EasyMockRule aware of EasyMockSupport.

Ideally, i'd like to have another EasyMockRule constructor with a second EasyMockSupport parameter. So we could write this:

public class MyClassTest {
    private EasyMockSupport easyMockSupport = new EasyMockSupport();
    @Rule public EasyMockRule easyMockRule = new EasyMockRule(this, easyMockSupport);
    // ...
}

Or an annotation might be a solution to make EasyMockRunner work with EasyMockSupport as well?

Thanks for your great work!

@henri-tremblay henri-tremblay added this to the 4.3 milestone May 8, 2020
@henri-tremblay
Copy link
Contributor

I had your solution with the constructor in mind. But you are right, it should work with the runner as well (and JUnit 5 extension)

@henri-tremblay henri-tremblay modified the milestones: 4.3, 4.4 Apr 15, 2021
@henri-tremblay henri-tremblay removed this from the 5.0.0 milestone Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants