-1

This is how I am mocking object of class Client present in another project:

@Mock
private Client client;

//Mocking method of class client - 

@Test
public void test()
{
    Mockito.when(client.getPassportDetail(Matchers.eq(bytes),Matchers.eq(properties)))
           .thenReturn(hash);
}

Structure of class Client:

class Client
{

    public static boolean loadLibraries(Properties properties) {
    }

    public HashMap<String, String> getPassportDetail(byte[] b, Properties properties) throws Exception{
        if (!loadLibraries(properties)) 
        {
            throw new UnsatisfiedLinkError();
        }
    }

So, when I mock getPassportDetail method, it gets called, not mocked.

Glenn
  • 8,932
  • 2
  • 41
  • 54
chitwan
  • 263
  • 2
  • 4
  • 15
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 10 '17 at 11:30
  • Your input is simply incomplete. Is it correct that the *Client* class is **not** public? – GhostCat Aug 10 '17 at 11:31

1 Answers1

1

That's a common mistake. Actually @Mock annotation need something more to work as You want to.

You have 3 options there:

Add

@RunWith(MockitoJUnitRunner.class) 

to Your test class.

Or

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

There is also 3rd option, that @Dawood ibn Kareem suggest in comment below:

There's a third option, which is better than either of these two. Use the new Mockito Rule - @Rule public MockitoRule rule = MockitoJUnit.rule();. More detail at my answer here, which also explains why this is the best of the three options.

That should be all.

You can always refer to http://www.baeldung.com/mockito-annotations for more informations and detailed explanation.

Lidjan
  • 154
  • 1
  • 2
  • 18
  • 1
    There's a third option, which is better than either of these two. Use the new Mockito Rule - `@Rule public MockitoRule rule = MockitoJUnit.rule();`. More detail at [my answer here](https://stackoverflow.com/a/10812752), which also explains why this is the best of the three options. – Dawood ibn Kareem Aug 10 '17 at 11:52
  • @DawoodibnKareem wow, I didn't know about it. Thanks for advice! :) – Lidjan Aug 10 '17 at 12:01