This is not how my code looks like, but it will come close to what i want as an example
class Phone {
public makeCall(String number) {
addEntryToCallLog(number)
//eventually make a call
}
private void addEntryToCallLog(String number) {
//make database calls
}
}
class PhoneTest {
Phone phone
@Test
public void canMakeACall() {
//mock the behaviour of phone.addEntryToCallLog to prevent database exceptions
phone.makeCall("1223");
//Assert somehow that call was made << --IGNORE this NOT IMPORTANT
}
}
I want to test "makeCall" in a unit test, but when i do so the code would throw some database exception and hence break my test. I think it is reasonable to be able to test mock the behaviour of java private methods since this allows for a consistent behaviour for all tests.
I have used groovy in the past and with it i could use spock to mock the behaviour of private methods. I could also use metaClasses to do the same for instances I create. But in java there doesnt seem to be a straight forward way to do this.
I have also tried mockito and power mockito but they allow me to change the return value of private methods, but they dont allow me to change the behaviour.
This seems to be an obvious thing that someone out there has had ti deal with. I suppose I am missing something. But what is it.