Let suppose, we have a class Employee having some private data members and public methods in it. I want to create a Junit test case to cover whether the method is called or not.
class Employee constructor(employeeName: String?){
private var isEligibleForPromotion = false
private var promotedPosition: PromotedPosition? = null
init {
try{
// checking If Employee is eligible for promotion
} catch() {}
}
fun givePromotion(employeeName: String?) {
if(isEligibleForPromotion) {
promotedPosition.promote(employeeName) //calls promote () in class PromotedPosition
}
}
}
Now, I would like to write a test case to ensure that, promotedPosition.promote() is called or not. But in order to achieve it, I need to mock the private variable isEligibleForPromotion because, I need to test it for both true & false.
Can anyone help me out in this.
I tried mocking and spying that class and private var isEligibleForPromotion. But unable to do it.