Sometime we have to test whether a method throws proper and required exceptions or not. One way of testing is placing the method call (to be tested) inside a try catch block, as shown below try { class2.method1(); fail(“Should have thrown exception”); } catch(IllegalArgumentException excep) { } catch(Exception excep) { fail(“Should have thrown IllegalArgumentException exception”);…… Continue reading Unit testing method exceptions
Category: JUnit
Setting timeout for test methods
Junit offers us the option to set timeout for test methods. If a test methods doesnt complete within specified time limit, an exception is thrown failing the test case. The use case scenario where this feature can be used is for example testing the performance of a method. The below code shows you how to…… Continue reading Setting timeout for test methods
Making JUnit ignore test methods
If we want JUnit to ignore certain test methods we can do that by using the @Ignore annotation applied on the methods that has to be ignored. The below test class tests a functionality of a calculator CalculatorTest package Package1; import static org.junit.Assert.assertEquals; import org.junit.Ignore; import org.junit.Test; @Ignore public class CalculatorTest { @Test public void…… Continue reading Making JUnit ignore test methods