mock(Class<T> classToMock, String name) name was ignored
See original GitHub issuei have run the test named “InjectionOfInlinedMockDeclarationIssueTest” and try to modfied the name of the mock failed with testing , by it always passed test
@RunWith(MockitoJUnitRunner.class)
public class InjectionOfInlinedMockDeclarationIssueTest {
@InjectMocks private Receiver receiver;
@InjectMocks private Receiver spiedReceiver;
private Antenna oldAntenna = mock(Antenna.class);
private Antenna satelliteAntenna = mock(Antenna.class);
/*opt1*/
private Antenna antenna = mock(Antenna.class,"dvbtAntenna");
/*opt2*/
private Antenna antenna = mock(Antenna.class,"testing");
/*opt3*/
private Antenna antenna = mock(Antenna.class);
@Test
public void named_mocks_should_be_resolved_with_their_name() throws Exception {
//should not same
assertSame(antenna , receiver.dvbtAntenna);
assertNotSame(antenna , oldAntenna);
assertNotSame(receiver , spiedReceiver);
assertNotSame(oldAntenna , satelliteAntenna);
assertNotSame(oldAntenna , antenna);
assertNotSame(satelliteAntenna , antenna);
}
// note that static class is not private !!
static class Receiver {
Antenna oldAntenna;
Antenna satelliteAntenna;
Antenna dvbtAntenna;
}
private static class Antenna { }
}
leave the opt1 or opt2 or opt3 alone , the test was always passed .
plz fix this bug
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (11 by maintainers)
Top Results From Across the Web
Why doesn't Mockito mock static methods? - Stack Overflow
The only way to mock statics is to modify a class' byte code at runtime, ... class Buddy { static String name() {...
Read more >Mockito (Mockito 4.9.0 API) - Javadoc.io
Mockito will now allow to ignore stubbing for the sake of verification. ... public static <T> T mock(Class<T> classToMock, String name).
Read more >Mocking Static Methods With Mockito - Baeldung
Explore a couple of examples of how we can use Mockito to mock ... ahead and see how we can mock the name...
Read more >Methods of Mockito - Javatpoint
Syntax: <T> mock(Class<T> classToMock, ReturnValues returnValues); mock() method with String: It is used to create mock objects by specifying the mock names ......
Read more >Mockito mock examples - DigitalOcean
We can use Mockito class mock() method to create a mock object of a given ... class Fruits{ private List<String> names; public List<String> ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@SeriyBg I think this is working as intended, but maybe one of the other @mockito/core can chime in as well.
If you would like to help us out, we are eagerly looking for contributions! Check out this list of issues to find an easy one to start (or participate in the discussion): https://github.com/mockito/mockito/issues?q=is%3Aissue+is%3Aopen+label%3A"please+contribute"
Sorry this took so long. As a matter of fact, I would suggest that Mockito should throw an exception if mocks of the same type are in conflict to one another. This is always better then undefined behavior.
We could also add a boolean property to
@InjectMocks
that allows to fall back to the old behavior but in this case, I would actually suggest to just throw the exception as this behavior might just break by an implementation detail in the future anyways.