views:

36

answers:

2
package com.fitaxis.test;

import java.sql.SQLException;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static org.mockito.Mockito.*;

import com.fitaxis.leaderboard.LeaderBoard;

public class LeaderBoardTests {


 @Test 
 public void TestThatDataIsSavedToTheDatabase()
 {
  LeaderBoard leaderBoard = mock(LeaderBoard.class);
  //doNothing().doThrow(new RuntimeException()).when(leaderBoard).saveData();

  when(leaderBoard.saveData()).thenReturn(true);

  boolean res = leaderBoard.saveData();

  verify(leaderBoard).saveData();

  Assert.assertTrue(res);
 }

}

I have used mockito to mock a class, but when I use code coverage it does not detect that the method as been called. Am I doing something wrong? Please help!

+1  A: 

It looks like you're mocking out the only call you're making to production code.

In other words, your test says:

  • When I call saveData(), fake the result to return true
  • Now call saveData() - yay, the result was true!

None of your production code is being calls at all, as far as I can see.

The point of mocking is to mock out dependencies from your production class, or (sometimes, though I prefer not to) to mock out some methods of your production class that the code you're actually testing will call.

You should probably be mocking out the dependencies of Leaderboard rather than Leaderboard itself. If you must mock out saveData(), you should be testing the methods that call saveData()... check that they save the right data, that they act correctly when saveData() returns false, etc.

Jon Skeet
Your correct, but I did the same thing with NUnit and RhinoMocks and NCover detected it as a call. I am wondering if I should use an interface sigh.
ferronrsmith
Seems I'm gonna have to do a integration test, I was trying to avoid hitting the DB for the test, but mock saving it to the DB, but it looks like to get code coverage I have to. Thanks for the help all :)
ferronrsmith
@ferronrsmith: You seem to be missing the thrust of my post: **your test is pointless**. It's not testing *any* of your code. What did you expect it to do? You shouldn't be writing tests to get code coverage; you should be writing them to exercise your code. The test you've presented doesn't do that. By all means mock out the saving part, but then call some other code which in turn calls `saveData()`. Simply calling `saveData()` directly is pointless. (I'm intrigues as to how you used NUnit and RhinoMocks when this is Java code, btw... You clearly weren't testing the same code.)
Jon Skeet
Thanks man, I fixed it, thanks for the help
ferronrsmith
Please give @Jon's answer a tick, as he helped you find a solution. And remember, the point of mocking is to give your class-under-test some fake collaborators. You get nothing by testing your mocks directly.
Synesso
+1  A: 

if i understand your question correctly :

because you are mocking LeaderBoard. that means that you are not testing it.

if you want to test LeaderBoard, you should test the actual class not the mocked one.

let say you want to test class A but this class depends on B and B is a bit difficult to instantiate in testing environment(for any reason). in such cases you can mock B.

but here is your case you are mocking class A itself. that means you are not testing anything.

mohammad shamsi