import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.fail;
public class TestingCoercion {
@Test
public void multiply2x2() {
MyClass myClass = new MyClass();
Assert.assertEquals(myClass.multiply(2, 2), 4);
}
@Test
public void multiplyDoesNotWorkWithFloat() {
MyClass myClass = new MyClass();
try {
myClass.multiply(2.0, 2); //this line does not compile
fail("MyClass.multiply is only for integers");
} catch (Exception exception) {
Assert.assertTrue("MyClass.multiply correctly rejected a double", true);
}
}
class MyClass {
public int multiply(int i, int j) {
return i * j;
}
}
}
try {
MyClass.class.getMethod("multiply", int.class, int.class);
} catch (NoSuchMethodException) {
// test has failed.
fail(); //do not swallow this exception, fail the test
}