JUnit Test 'assertNotEquals' Example Using Ant

In the previous example we explain how to use of assertEquals assertion method, in this example we try to illustrates about how to use the 'assertNotEquals' assertion method to test whether the class file executed test cases successfully or not.

JUnit Test 'assertNotEquals' Example Using Ant

JUnit Test 'assertNotEquals' Example Using Ant

     

In the previous example we explain how to use of assertEquals assertion method, in this example we try to illustrates about how to use the 'assertNotEquals' assertion method to test whether the class file executed test cases successfully or not. 

The assertNotEquals assertion method is used to compare between two variable whether both are equal or not, if both 'expected' and 'actual' are equal then the AssertionError Exception throw the following error:

[junit] Testsuite: AssertNotEqualsExample
[junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0.062 sec
[junit]
[junit] Testcase: test(AssertNotEqualsExample): FAILED
[junit] expected not equals to: <2>
[junit] junit.framework.AssertionFailedError: expected not equals to: <2>
[junit] at junitx.framework.Assert.failNotEquals(Assert.java:354)
[junit] at junitx.framework.Assert.assertNotEquals(Assert.java:132)
[junit] at junitx.framework.Assert.assertNotEquals(Assert.java:248)
[junit] at junitx.framework.Assert.assertNotEquals(Assert.java:257)
[junit] at AssertNotEqualsExample.test(Unknown Source)
[junit]
[junit]
[junit] Test AssertNotEqualsExample FAILED

but if both are not equal then the program executed test case successfully. In this example build.xml file is used to compile, test and run the java file.

build.xml

 

<project name="JUnitTest" default="test" basedir=".">

  <property name="testdir" location="test" />
  
  <path id="classpath.base"/>
  <path id="classpath.test">
  <pathelement location="${testdir}" />
  <path refid="classpath.base" />
  </path>

  <target name="clean" >
  <delete>
  <fileset dir="${testdir}" includes="**/*.class" />
  </delete>
  </target>

  <target name="compile" depends="clean">
  <javac srcdir="${testdir}" >
  <classpath refid="classpath.test"/>
  </javac>
  </target>

  <target name="test" depends="compile">
  <junit>
  <classpath refid="classpath.test" />
  <formatter type="brief" usefile="false" />
  <test name="AssertNotEqualsExample" />
  </junit>
  </target>

</project>


Source Code of AssertNotEqualsExample.java

 

import java.lang.*;
import junit.framework.*;

public class AssertNotEqualsExample extends TestCase{

  public void test(){
  int expected = 2;
  int actual = 2;
  try{
  junitx.framework.Assert.assertNotEquals(expected, actual);
  }catch(AssertionError exception){ }
  }

  public void test01(){
  int expected = 2;
  int actual = 4;
  try{
junitx.framework.Assert.assertNotEquals("not equal", expected, actual);
  }catch(AssertionError exception){ }
  }
}

output:

 

Download Source Code