samedi 7 mars 2015

How would you handle private methods when mocking?

I am trying to understand how to use mocking with Mockito, but I don't know how to handle the following situation:



  1. Where the method in the class which you are trying to mock is private.

  2. Where the method should remain in the class.

  3. Where making the method public could result in future programmers making the code behavior unexpectedly, thus should remain private.




public class Foo {

private final int secret;

public Foo(int s) {
secret = s;
}

public void bar() {
int sum = sum(0);
}

private int sum(int i) {
if (i == 1) {
return i * secret;
}
secret -= i;
return i;
}
}


I am trying to mock Foo and test that sum is behaving correctly, but I'm struggling to decide how best to do so with minimal impact. Should I change the sum method to public? How do you go about testing this behavior?


Aucun commentaire:

Enregistrer un commentaire