Skip to content

Instantly share code, notes, and snippets.

@luisartola
Last active January 25, 2017 19:39
Show Gist options
  • Save luisartola/bd49718d18f8e738c037be976ace0786 to your computer and use it in GitHub Desktop.
Save luisartola/bd49718d18f8e738c037be976ace0786 to your computer and use it in GitHub Desktop.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExampleTest {
/*
Sometimes(*) 100% line coverage is not enough
*/
@Test
public void one_case_that_covers_all_the_lines() {
assertEquals("50.0 %", this.formattedPercentage(5, 10));
}
private String formattedPercentage(double x, double y) {
return String.valueOf((x / y) * 100) + " %"; //if y == 0 it fails, no test covers this
}
}
-// (*) Achieving 100% coverage can be really hard. You need to think about the ROI (economic!) of the tests you are writing
-// Don´t think about "line coverage", think about 100% of input/output combination cases
@lluismf
Copy link

lluismf commented Jan 25, 2017

	@Test
	public void one_case_that_covers_all_the_lines() {
		assertEquals("50.0 %", this.formattedPercentage(5, 10));
	}

	@Test(expected = ArithmeticException.class)
	public void divide_by_zero() {
		this.formattedPercentage(5, 0);
	}

	private String formattedPercentage(double x, double y) throws ArithmeticException {
		if (y == 0) {
			throw new ArithmeticException();
		}
		return String.valueOf((x / y) * 100) + " %"; // if y == 0 it fails, no test covers this
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment