Percentage { public static double perc( double hits, double total ) throws NoPercentException { if ( total == 0 ) { throw new NoPercentException( "none (total=zero)" ); } if ( hits > total ) { throw new NoPercentException( "none (hits > total)" ); } return 100 * ( hits / total ); }  static String format( double hits , double total ); static double perc( double hits, double total );  }
@Test public void testPercValues() throws Exception { assertEquals( 0, Percentage.perc( 0, 100 ), 0.0 ); assertEquals( 5, Percentage.perc( 5, 100 ), 0.0 ); assertEquals( 50, Percentage.perc( 50, 100 ), 0.0 ); assertEquals( 100, Percentage.perc( 100, 100 ), 0.0 ); }
@Test( expected = NoPercentException.class ) public void testFailed() throws Exception { Percentage.perc( 100, 2 ); }
@Test( expected = NoPercentException.class ) public void testZeroTotal() throws Exception { Percentage.perc( 100, 0 ); }