简体   繁体   中英

Mockito @InjectMocks Strategy

Yes, I know, much has been written about Mockito's @InjectMocks, but there is still a problem I haven't been able to solve...

Let's assume we have a class with four members...

class A {

    public B b1;
    public B b2;
    public B b3;
    public B b4;

}

...with B being an empty interface ...

interface B {

}

...then this setup will fail to initialize properly:

class MyTest {

    @InjectMocks
    private A a;

    @Mock(name = "b2")
    private B b2;  // will correctly be injected into field b2

    @Mock(name = "b4")
    private B b4;  // will be injected into field b3

    @Before
    public void init() {
        MockitoAnnotations.initMocks( this );
    }

    // some test here

}

Why? I have debugged the whole thing and found that it goes this way...

  1. Search for a proper mock for field b1 . Two candidates found with suitable types, so look for names. None of the names match. End. b1 is empty, correct.
  2. Search for a proper mock for field b2 . Two candidates found with suitable types, so look for names. One of the names match. b2 is filled correctly with the mock "b2".
  3. Search for a proper mock for field b3 . Only one candidate ("b4") is found with suitable types, so use that one. b3 is incorrectly filled with mock "b4".
  4. No mock left for field b4 , will remain empty (incorrectly).

So, the only solution would be to add always one mock more than needed (so that for all needed mocks, there's always at least 2 candidates):

@Mock(name = "fakeyMcFakeFake")
private B fakeyMcFakeFake;

( Of course, fakeyMcFakeFake will be injected into field b1 in the second pass, but at least the other fields will be filled with the correct mocks ). But can this really be a solution? Has anyone an idea why this isn't handled better or how to handle it better?

( As for the use case... I have a controller that has various textboxes, etc. as members (those will be injected in the live code), obviously many of them are of the same type but for each test I will only need a small number of them.)

This has been fixed in pull request #215 (see release notes ). I suspect your code is about an older version of mockito.

While I like powermock I would advise to design code in a way that testing with mocks don't rely on powermock.

Also for powermock, either upgrade to version 1.6.4 (just released) ; there's an issue ( #14 ) on powermock github repository that changes the dependency to mockito-core . Otherwise please exclude mockito-all when declaring powermock dependency, mockito-all is a dependency for non-maven users (that still use the central repository). However I'm not sure future mockito 2.x beta will be compatible with current (1.6.x) powermock version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM