Skip to content

Instantly share code, notes, and snippets.

@segunfamisa
Created May 9, 2017 22:48
Show Gist options
  • Save segunfamisa/55808c1b6858b1b5ee8fb09e7986299d to your computer and use it in GitHub Desktop.
Save segunfamisa/55808c1b6858b1b5ee8fb09e7986299d to your computer and use it in GitHub Desktop.
Gist describing a potential usecase for VisibileForTesting annotation https://developer.android.com/reference/android/support/annotation/VisibleForTesting.html
public class Calculator {
private int a;
private int b;
public Calculator(int a, int b) {
this.a = a;
this.b = b;
}
public void sum() {
doSum(a,b);
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
protected int doSum(int a, int b) {
return a + b;
}
}
public class CalculatorTest {
@Test
public void testSum() {
// given a, b & calculator instance
int a = 1;
int b = 3;
Calculator calc = new Calculator(a,b);
// when we do sum
int sum = calc.doSum(a,b);
// then verify that sum is 4
assertEquals(sum, 4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment