'org.junit.Assert'
class provides number of assertion methods to verify the expected values. Assert class
uses hamcrest library internally. If you would like to know about hamcrest
library, I would recommend you go through my below tutorial series.
ArithmeticTest.java
The
format of all assertions looks like below.
assertionMethod(expectedValue,
actualValue)
assertionMethod(message,
expectedValue, actualValue)
‘message’ will be displayed on failures.
Let’s
see it by an example.
To demonstrate this example, I implemented add method wrongly.
Arithmetic.java
package com.sample.arithmetic; public class Arithmetic { public int add(int a, int b) { return a + b + b; } }
ArithmeticTest.java
package com.sample.arithmetic; import static org.junit.Assert.assertEquals; import org.junit.Test; /** * Test cases follow below naming convention. methodName_input_output format. * * @author krishna * */ public class ArithmeticTest { @Test public void divide_10Plus7_17() { Arithmetic obj1 = new Arithmetic(); int actual = obj1.add(10, 7); int expected = 17; assertEquals("Addition of 10, 7 is failing", expected, actual); } }
When
I ran, ArithmeticTest.java, I can able to see the error message.
In
my next posts, I will explain all the assert methods in detail.
No comments:
Post a Comment