-1

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 :)

hqt
  • 29,632
  • 51
  • 171
  • 250

1 Answers1

1

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().

Artur Zagretdinov
  • 2,034
  • 13
  • 22
  • So we still test `A_C1()` `A_C2()` `B_C1()` `B_C2()` But use some methodologies you have mentioned for saving repeated code. right? – hqt Apr 08 '16 at 02:50
  • If code paths in `A()` and `B()` doesn't depend on `C()` then - no, you will test only `A`, `B`, `C_1, `C_2`. I've create a [code examples](https://github.com/thekingnothing/stackoverflow/tree/master/q36482557) hot it can look like in different cases – Artur Zagretdinov Apr 08 '16 at 07:58