Use 'Assert.assertEquals' to test method using Junit framework
Source: guru99.com
JUnit assertEquals
- You have assertEquals(a,b) which relies on the equals() method of the Object class.
- Here it will be evaluated as a.equals( b ).
- Here the class under test is used to determine a suitable equality relation.
- If a class does not override the equals() method of Object class, it will get the default behaviour of equals() method, i.e. object identity.
If a and b are primitives such as byte, int, boolean, etc. then the following will be done for assertEquals(a,b) :a and b will be converted to their equivalent wrapper object type (Byte,Integer, Boolean, etc.), and then a.equals( b ) will be evaluated.For Example: Consider below-mentioned strings having same values, let's test it using assertTrueString obj1="Junit"; String obj2="Junit"; assertEquals(obj1,obj2);
Above assert statement will return true as obj1.equals(obj2) returns true.Floating point assertions
When you want to compare floating point types (e.g. double or float), you need an additional required parameter delta to avoid problems with round-off errors while doing floating point comparisons.The assertion evaluates as given below:- Math.abs( expected – actual ) <= delta
For example:assertEquals( aDoubleValue, anotherDoubleValue, 0.001 )
Comments
Post a Comment