question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mock(Class<T> classToMock, String name) name was ignored

See original GitHub issue

i 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:open
  • Created 6 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
TimvdLippecommented, Dec 7, 2017

@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"

0reactions
raphwcommented, Feb 18, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found