0

I have a method like following

  public void init() {
        String message = getApplication().getString(R.string.empty_message_with_user,user.getUsername());
        emptyMessageText = new ObservableField<>(message);
    }

I tested this method like following

@Test
public void init_isCalled() {
    MyClass myClass = mock(MyClass.class);
    doNothing().when(myClass).init();
    myClass.init();

    verify(myClass, times(1)).init();
 }

The above test run fine , but problem is when I look the coverage report , it shows that method is not covered by Test, I generate coverage report with jacoco.

mW3
  • 189
  • 3
  • 25
  • 3
    Mocking consists in replacing a method by a fake one. So if you mock the method you're testing, you're not testing your code anymore. You're testing Mockito. This makes no sense. All the test does is checking that calling init() calls init(). – JB Nizet Mar 14 '19 at 12:17
  • @JBNizet thanks for your comment, I am kind of new to TDD , how can I write the real test for this method . Any help would be highly appreciated . – mW3 Mar 14 '19 at 12:27
  • Create your object, call your init() method on this object, and check that its emptyMessageText property contains the expected value. – JB Nizet Mar 14 '19 at 12:31
  • See https://stackoverflow.com/questions/8751553/how-to-write-a-unit-test – Raedwald Mar 14 '19 at 12:32

2 Answers2

0

You can mock init method when you're creating a test where it's calling from another method.

But if you want to test init method itself (and get its coverage), you don't need to mock it. Simply call it from your test. In turn, you can mock getApplication() call if needed.

amseager
  • 5,795
  • 4
  • 24
  • 47
0

When we look at your class MyClass and how it would later be used, this would have the following structure:

+---------------+           +--------------+           +----------------+
|Production Code| +-------> |MyClass.init()| +-------> |getApplication()|
+---------------+           +--------------+           +----------------+

Now, to test the MyClass.init() method, a useful test setup might look as follows:

+---------+                 +--------------+           +-----------------------+
|Test Code| +-------------> |MyClass.init()| +-------> |mocked getApplication()|
+---------+                 +--------------+           +-----------------------+

The test code would call the MyClass.init() method, because that is the method you want to test. You don't want to test the other classes / methods that are called by MyClass.init(), ahd thus it might be a good option to mock these. (Note that this is not mandatory, sometimes you just can also reach all your unit-testing goals with the other components just used as they are without mocking.)

However, what you have done is the following:

+---------+                 +---------------------+
|Test Code| +-------------> |mocked MyClass.init()|
+---------+                 +---------------------+

And now you can clearly see, why you also do not see any covered lines in the original MyClass.init(), because that just does not get called.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47