Я пишу свой первый тестовый класс junit. Мой класс calculator.java находится в моей директории src, а мой тестовый класс (CalculatorTest.java) находится в каталоге test/src. Моя проблема в том, что класс CalculatorTest не распознает класс калькулятора. Есть предположения? Вот это мой calculator.java класс в каталоге Src:JUnit TestClass не распознает класс
package edu.umb.cs.cs680.unittest;
public class Calculator {
public float multiply(float x, float y){
return x*y;
}
public float divide(float x, float y){
if(y==0){
throw new IllegalArgumentException("division by zero");
}
return x/y;
}
}
И это мой CalculatorTest класс в каталоге тест/Src:
package edu.umb.cs.cs680.unittest;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;
import edu.umb.cs.cs680.unittest.Calculator;
public class CalculatorTest{
@Test
public void multiply3By4(){
Calculator cut = new Calculator();
float expected = 12;
float actual = cut.multiply(3,4);
assertThat(actual, is(expected));
}
@Test
public void divide3By2(){
Calculator cut = new Calculator();
float expected = 1.5f;
float actual = cut.divide(3,2);
assertThat(actual, is(expected));
}
@Test(expected=IllegalArgumentException.class)
public void divide5By0(){
Calculator cut = new Calculator();
cut.divide(5,0);
}
}
Это мой build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project default="runjunit" name="ant project">
<property environment="env"></property>
<property name="ECLIPSE_HOME" value="${env.ECLIPSE_HOME}"></property>
<property name="junit.output.dir" value="test/bin/edu/umb/cs/cs680/unittest"></property>
<property name="bin.dir" value="bin/edu/umb/cs/cs680/unittest"></property>
<path id="JUnit 4.libraryclasspath">
<pathelement location="lib/junit.jar"/>
<pathelement location="lib/org.hamcrest.core_1.3.0.v201303031735.jar"/>
</path>
<path id="HW4.classpath">
<path refid="JUnit 4.libraryclasspath"/>
</path>
<target name="compile">
<mkdir dir="${bin.dir}"/>
<mkdir dir="${junit.output.dir}"/>
<javac includeantruntime="false" srcdir="./src" destdir="./bin" >
<classpath refid="HW4.classpath"></classpath>
</javac>
<javac includeantruntime="false" srcdir="./test/src" destdir="./test/bin" >
<classpath refid="HW4.classpath"></classpath>
</javac>
</target>
<target name="clean">
<delete dir="./bin"/>
<delete dir="./test/bin"/>
</target>
<target name="runjunit" depends="compile">
<junit printsummary="yes" haltonfailure="yes">
<path refid="JUnit 4.libraryclasspath" />
<!--<test name="test.src.edu.umb.cs.cs680.unittest.CalculatorTest" />-->
<test name="x.CalculatorTest" />
<classpath refid="HW4.classpath"></classpath>
</junit>
</target>
</project>
Это ошибка:
cannot find symbol Calculator cut = new Calculator();
Благодаря
Возможно, у вашего пути есть недостаток. –
Являются ли ваши оба класса в разных пакетах? –