Why do I get a Null Pointer in my test whenever I run it from the ant?My Test class Is:
package com.MySelf.exchangerate.exchange;
import static org.junit.Assert.assertEquals;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import junit.framework.Test;
import com.MySelf.exchangerate.application.ExchangeRateApplication;
import junit.framework.TestSuite;
public class ExchangeRateApplicationTestCase extends AbstractTestCase{
public static final Logger logger=Logger.getLogger(ExchangeRateApplicationTestCase.class);
private static final double delta = 1e-15;
private ExchangeRateApplication exchangeRateApplication;
private static String curCon,inpCur;
public ExchangeRateApplicationTestCase( String testName ) {
super( testName );
}
protected void tearDown()throws Exception{
logger.debug("In method teardDown() which has annotation @after" );
super.tearDown();
}
public void testExchangercase() {
int expected=20;
int actual = exchangeRateApplication.exchangercase();
assertEquals(expected,actual);
}
public static Test suite() {
setLog4J();
logger.info( "In suite" );
TestSuite testSuite = new TestSuite();
testSuite.addTest(
new ExchangeRateApplicationTestCase(
"testExchangercase" ) );
return testSuite;
}
public static void main( String args[]){
System.setProperty( "log4j.configuration",
"C:/javafundamentals/MultiAntLatest2/" +
"ant_template_core_java/SVN_LOCAL/log4j/log4j.xml" );
BasicConfigurator.configure();
//junit.textui.TestRunner.run(new TestSuite( ExchangeRateApplicationTestCase.class ) );
junit.textui.TestRunner.run( suite() );
}
}
My Application is:
package com.MySelf.exchangerate.application;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import com.MySelf.exchangerate.interfaces.Convert;
public class ExchangeRateApplication implements Convert{
Logger logger = Logger.getLogger(ExchangeRateApplication.class);
private double rate;
private double startingAmt, inpAmt;
private String curCon, fromStr, toStr;
private String inpCur;
public void setRate(String toStr, double rate){
this.toStr = toStr;
this.rate = rate;
}
public ExchangeRateApplication(String inpCur, String curCon){
this.inpCur = inpCur;
this.curCon = curCon;
}
public String getFromStr(){
return fromStr;
}
public String getToStr(){
return toStr;
}
public int exchangercase(){
int rates=10;
int inpAmnt=2;
int conversion=rates*inpAmnt;
return conversion;
}
public double exchanger (String inpCur, String curCon, double inpAmt){
this.inpCur = inpCur;
this.curCon = curCon;
this.inpAmt = inpAmt;
return inpAmt * rate;
}
public void inputOutput(){
ArrayList a =new ArrayList();
boolean keepPlaying = true;
int selection;
do{
String inpCur = JOptionPane.showInputDialog(null, "Enter the type of currency you want to convert from:");
String curCon = JOptionPane.showInputDialog(null, "Enter the type of currency you want to convert to:");
String inpAmt = JOptionPane.showInputDialog(null, "Enter the amount you want to convert:");
double conv = Double.parseDouble(inpAmt);
a.add(inpCur);
a.add(curCon);
a.add(inpAmt);
String setRate = JOptionPane.showInputDialog(null, "Set the rate of the currency you are going to convert to:");
double rate = Double.parseDouble(setRate);
a.add(rate);
ExchangeRateApplication exchangeRateApplication = new ExchangeRateApplication(inpCur, curCon);
exchangeRateApplication.setRate(curCon, rate);
//System.out.print(inpAmt + " " + inpCur + " converted to " + curCon + " is " + exchangeRateApplication.exchanger(inpCur, curCon, conv) + " " + curCon);
JOptionPane.showMessageDialog(null,inpAmt + " " + inpCur + " converted to " + curCon + " is " + exchangeRateApplication.exchanger(inpCur, curCon, conv) + " " + curCon);
selection = JOptionPane.showConfirmDialog(null, "Would You Like to Convert Again?", "Confirmation", JOptionPane.YES_NO_OPTION);
}
while(keepPlaying = (selection == JOptionPane.YES_OPTION));
JOptionPane.showMessageDialog(null, "\nThank you for using the converter!!!");
System.exit(0);
}
public static void main(String[]args){
String log4jXmlPath
= System.getProperty( "log4j.configuration" );
System.out.println("log4jXmlPath = " + log4jXmlPath);
if ( log4jXmlPath != null ) {
DOMConfigurator.configure(log4jXmlPath);
}
else {
BasicConfigurator.configure();
}
Convert convert=new ExchangeRateApplication("USD","Kes");
convert.inputOutput();
}
}
My Junit.Xml is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="junit" basedir=".">
<path id="classpath.junit">
<pathelement location="${absolute.junit.build.classes.dir}"/>
<fileset dir="${absolute.static.lib.dir}">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${absolute.command.dist.dir}">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${absolute.application.dist.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="junit.compile">
<javac destdir="${absolute.junit.build.classes.dir}"
nowarn="${build.compiler.nowarn}"
debug="${build.compiler.compile.with.debug}">
<src path="${absolute.junit.src.dir}"/>
<classpath>
<path refid="classpath.junit"/>
</classpath>
</javac>
<copy todir="${absolute.junit.build.classes.dir}">
<fileset dir="${absolute.junit.src.dir}">
<include name="**/*.ear"/>
</fileset>
</copy>
</target>
<target name="junit.run.tests" depends="build" description="run tests">
<junit printsummary="yes" haltonfailure="no" failureproperty="junit_failed">
<sysproperty key="log4j.configuration"
value="${log4j.configuration}"/>
<classpath refid="classpath.junit" />
<formatter type="xml"/>
<batchtest fork="yes" todir="${absolute.junit.data.dir}"
failureproperty="junitfailed">
<fileset dir="${absolute.junit.build.classes.dir}">
<include name="**/*Test*.class"/>
<exclude name="**/AbstractTestCase.class"/>
</fileset>
</batchtest>
</junit>
<antcall target="junit.make.report"/>
<fail message="JUnit tests failed" if="junitfailed" />
</target>
<target name="junit.run.tests.debug" depends="build" description="run tests in debug mode">
<junit printsummary="yes" haltonfailure="no" failureproperty="junit_failed">
<sysproperty key="log4j.configuration"
value="${log4j.configuration}"/>
<jvmarg value="-Xdebug"/>
<jvmarg value="-Xnoagent"/>
<jvmarg value="-Djava.compiler=NONE"/>
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787"/>
<classpath refid="classpath.junit" />
<formatter type="xml"/>
<batchtest fork="yes" todir="${absolute.junit.data.dir}">
<fileset dir="${absolute.junit.build.classes.dir}">
<include name="**/*Test*.class"/>
<exclude name="**/AbstractTestCase.class"/>
</fileset>
</batchtest>
</junit>
<antcall target="junit.make.report"/>
<fail message="JUnit tests failed" if="junitfailed" />
</target>
<target name="junit.make.report">
<junitreport todir="${absolute.junit.reports.dir}">
<fileset dir="${absolute.junit.data.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${absolute.junit.reports.dir}"/>
</junitreport>
</target>
</project>
I get the below error on running the tests:
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.141 sec
[junit] Test com.MySelf.exchangerate.exchange.ExchangeRateApplicationTestC
FAILED