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

@Mock default name is not used in failure messages #520

Open
sloescape opened this issue Jan 19, 2024 · 4 comments
Open

@Mock default name is not used in failure messages #520

sloescape opened this issue Jan 19, 2024 · 4 comments

Comments

@sloescape
Copy link

Although @mock usefully generates a default name for a mock using the field name, this name is not used when reporting failures, for example in verify failures. The issue is in Invocation.getMockAndMethodName which does not use the generated name for the mock if it does not look like a java identifier, which the generated name does not. As a result I believe the value of the @mock facility is reduced because it requires the manual addition of a 'name' attribute to differentiate mocks of the same type which in almost all cases will be the same as the field name.

@henri-tremblay
Copy link
Contributor

But a field name should be a Java identifier no?

@sloescape
Copy link
Author

It is the generated mock name that does not pass the check for a valid java identifier. Invocation.getMockAndMethodName has 'String mockName = mock.toString();' which gives a default mockName of "EasyMock for ...." which is not a valid java identifier as it include space characters.

@henri-tremblay
Copy link
Contributor

Can you give me a quick test case for that? Because I remember changing something in that realm. So maybe it's fixed now.

@sloescape
Copy link
Author

A simple test case with two tests. In both cases the error refers to the failing mock as 'Runnable' instead of 'myRunner1' or 'myRunner2':

package test;

import org.easymock.EasyMockExtension;
import org.easymock.EasyMockSupport;
import org.easymock.Mock;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith({EasyMockExtension.class})
public class MockTest extends EasyMockSupport {

@Mock
private Runnable myRunner1;

@Mock
private Runnable myRunner2;

@AfterEach
public void verify() {
    verifyAll();
}

@Test
public void testUnexpected() {
    replayAll();
    myRunner1.run();
}

@Test
public void testFailsVerify() {
    myRunner2.run();
    replayAll();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants