Skip to content

Instantly share code, notes, and snippets.

@fourlastor
Created June 24, 2015 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourlastor/208c02bcf6e5945a2fe6 to your computer and use it in GitHub Desktop.
Save fourlastor/208c02bcf6e5945a2fe6 to your computer and use it in GitHub Desktop.
coverage
/*
* Original requirement:
* if a > b, return 1, if a == b return 0, if a < b return -1
*/
/*
* Day 1
* Paul comes in and does untested code which has a bug
*/
class UntestedClass {
public int compare(int a, int b) {
if (a > b) {
return -1;
}
if (a == b) {
return 0
}
return 1;
}
}
/*
* Day 2
* Nelson comes in and checks the code coverage.
* OH NOES! THERE ARE NO TESTS!
*/
class UntestedClassTest {
private UntestedClass theClass = new UntestedClass();
@Test
public void testAgtB throws Exception {
assertThat(theClass.compare(5, 3), is(-1));
}
@Test
public void testAeqB throws Exception {
assertThat(theClass.compare(3, 3), is(0));
}
@Test
public void testAltB throws Exception {
assertThat(theClass.compare(3, 5), is(1));
}
}
// Now the code is covered, and the bug looks like a requirement for future developers...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment