Search code examples
androidunit-testingtestingpowermockito

How to test in scenario many methods calling one helper method


For example here is my scenario:

function A() {
   C();
}

function B() {
   C();
}

function C() {
   if (someState > 0) then doSomething();
   else doSomethingElse();
}

I want to make all test case that coverage all code. Because C() has a condition so for testing C(), we need two testing method: testC1() and testC2(). So the total test is: testA_C1() testA_C2() testB_C1() testB_C1(). Number of testing methods will be increased dramatically when there are more conditions, and there are more methods that use same method C()

The problem here is: C() is not depend on any state of A() and B(), so in fact I think C() can be tested separately. So I think we can save a big amount of unit test.

My question is: How can I test in this scenario. I'm using Powermock for Android Testing.

Thanks :)


Solution

  • If as you said the c() doesn't depends on state of a() and b() then you can make extract method object refactoring and test it separately. In case if you use factory then you won't need PowerMock.

    If a() and b() doesn't depends on c() result then the suppress method can be used.

    If a() and b() depends on c() result, then you can create partially mock (by using spy) and mock the c().