Blog Archives

Failing unit tests with mockito


I posted a question here

Mockito will report an exception in calling class and fail the test -but it would not print the trace. Check out for two stuff:

  1. toString -can it result in NPTR? Do not print the field values blindly, have a NULLability check there.
  2. equals -While mocking, we pay not initialize the ID, for example if ID is a DB generated, then it’s NOT
    part of constructor and one can easily forget to assign it..
  3. Check for exception which can occur in calling module -may be the problem is as simple as trace not being printed!
Advertisements

Mock annotation returning null for mocked object


Using mockito and had written

@Mock
private SomeService someService;

This was returning NULL and my test cases were failing -What was the missing element – a one liner

@RunWith(MockitoJUnitRunner.class)
public class SomeClass{

Which is needed for @Mock annotation to work!