JUnit Tutorial
Introduction to Junit JUnit Set up JUnit Architecture JUnit Annotations JUnit Fixtures Junit Assertions Junit Categories @Test Annotation Parameters Verification of Exceptions Ignoring tests Time out in JUnit tests Parameterizing tests Test Suite TestWatcher TemporaryFolder ExternalResource Theories in JUnit JUnit Test Runners Execution order of JUnit tests Assumptions in JUnit JUnit and Hamcrest Matchers Running JUnit tests in parallel JUnit and Maven Integration JUnit and Gradle Integration Executing Selenium tests using JUnit test framework Method interceptor and usage JUnit in Intellij IDEA JUnit in EclipseVerification of exceptions in junit
Generally whenever a test throws any exception, JUnit fails that test. But some times the exceptions are expected So test should pass when the exception occur. Below example will illustrate how to test exceptions in JUnit tests. Note how we have used expected parameter in @Test annotation.
@Test(expected = ArithmeticException.class)
public void testException(){
System.out.println("Running Exception test");
int k = 0 ;
int b = 100/k;
}
Above test will pass after execution as the Arithmetic exception is thrown in last line. So that’s how we can check for any kind of exception in JUnit tests.Web development and Automation testing
solutions delivered!!