Base class
class BaseSplitter{
public int splitSalaryHalf(String personName,int Salary){
//check if person is employee
//check is person eligible
//some more check if yes
//split salary into salary/2 else return -1
return salary/2 or -1
}
public int abc(){
}
}
Derived class
class Bonus extends BaseSplitter{
public int giveBonous(String personName,int salary){
int bonusamount = super.splitsalaryHalf("Sunil",super.splitSalary("Sunil",salary));
if(bonusamount == -1){
return 0;
}
return Salary + bonusamount
}
}
I am trying to test giveBonus function with mockito but failing
public class Bonus {
@Test
public void giveBonous() throws Exception{
GiveBonus bonus = Mockito.spy(new Bonus);
doReturn(500).when((BaseSplitter)Bonus).splitSalaryHalf("sunil",2000);
int num = bonus.giveBonus("sunil",2000);
assertEqual(2500,num);
}
}
Question # 1
The issue is that mockito is not mocking the superclass(splitHalfSalary
) call rather start calling it in real (splitSalaryHalf
function which contains some complex object{not included to keep it simple} ). How can i mock superclass method.
Question # 2
Not applying a composition as I need abc()
and other functions in the derived class. Am I applying wrong pattern if Yes? how should I handle the same?