3:38 pm - Sunday June 25, 2017

Testing Exception with JUNIT

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Testing Exception with Junit

This tutorial will explain the how to check the expected exception throws in a function.

The expected parameter is used along with @Test annotation like @Test(expected)in action.

Example:Jex.java:

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class Jex {
	String s = "Ebullitent";
	JExcTest o = new JExcTest(s);
@Test(expected = ArithmeticException.class)   //Testing Exception
	public void print()
{
	System.out.println("am Print method");
	o.msg();
}
@Test
	public void result()
{
	System.out.println("Am Result method");
	s= "Hi!" + "welcome";
	assertEquals(s,o.disply());
	}

}

JExTest.java:


public class JExcTest {
private String s;
public JExcTest(String s)
{
}
System.out.println("am construct "+s);
public void msg()
{
	int a =0;
	int b = 1/a;
}
public String disply()
{
	s = "Hi!"+"welcome";
	System.out.println(s);
	return s;
	}
}

JuRun.java:


import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class JuRun{
	public static void main(String[] args) {
		Result result = JUnitCore.runClasses(Jex.class);
		for (Failure failure : result.getFailures()) {
			System.out.println(failure.toString());
			}
		System.out.println(result.wasSuccessful());
		}
}

Output

am construct Ebullitent
Am Result method
Hi!welcome
am construct Ebullitent
am Print method

BACK

Filed in: Z-A All Java Codes

Comments are closed.