Share on Google+Share on Google+

JUnit Test 'assertNotSame' Example Using Ant

As like the previous example it illustrates about how to use the 'assertNotSame' assertion method.

JUnit Test 'assertNotSame' Example Using Ant


As like the previous example it illustrates about how to use the 'assertNotSame' assertion method. The method is used to compare between two variable whether both are same or not, if both 'expected' and 'actual' are not same then the AssertionError Exception throw the following error:

[junit] Testsuite: AssertNotSameExample
[junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0.046 sec
[junit] Testcase: test(AssertNotSameExample): FAILED
[junit] expected not same
[junit] junit.framework.AssertionFailedError: expected not same
[junit] at AssertNotSameExample.test(Unknown Source)
[junit] Test AssertNotSameExample FAILED

but if both are same then the program executed test case successfully. In this example build.xml file is used to compile, test and run the java file. The <property name="TALK" value ="true"/> is used to search path for source files, search path for class files, parsing the jar file, loading the all required classes and checking the class file. If  <property name="TALK" value ="true"/> is not used in build.xml file then the output displays only  [junit] Testsuite



<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" />

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

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

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


Source Code of

import junit.framework.*;

public class AssertNotSameExample extends TestCase{

  public void test(){
  int expected = 2;
  int actual = 2;
  assertNotSame(expected, actual);
  }catch(AssertionError exception){ }

  public void test01(){
  int expected = 2;
  int actual = 4;
  assertNotSame("not same", expected, actual);
  }catch(AssertionError exception){ }



Download Source Code



Posted on: September 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JUnit Test 'assertNotSame' Example Using Ant  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image