Category Archives: TDD

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!

Mock annotation returning null for mocked object

Using mockito and had written

private SomeService someService;

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

public class SomeClass{

Which is needed for @Mock annotation to work!

Testing the DB layer

How to write Junits for DB layer?

One fundamental concept of TDD is that tests should not be interdependent –how do we apply this for DB, A typical step for DB layer test is –emphasizing am talking about the DB testing:

Test that DB server is up
Test that DB exists
Test that Table exists
Add a record

If the first one fails, all other are bound to fail.

For integration tests we can mock these aspects and get going. For now my tests are interdependent and if one fails all fails –indicating that DB layer is not UP. Am continuing till I get a better solution.