我正在编写我的第一个junit测试类。我的calculator.java类在我的src目录中,我的测试类(CalculatorTest.java)在test / src目录中。我的问题是CalculatorTest类无法识别计算器类。有什么想法吗?这是我在src目录中的calculator.java类:
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;
}
}
它是test / src目录中的CalculatorTest类:
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();
由于
答案 0 :(得分:1)
您需要导入课程Calculator
通常,应用程序类位于源或src/main
文件夹下,而测试类位于src/test
目录中。
因此,您必须确保在您的测试类中导入Calculator
类,即CalculatorTest
。
答案 1 :(得分:1)
我的calculator.java类在我的src direcotry和我的测试类中 (CalculatorTest.java)位于test / src目录中。我的问题是 CalculatorTest类无法识别计算器类。任何 想法?
您的文件夹结构似乎不正确,您需要具有如下所示的文件夹结构:
Calculator.java
- &gt; src/main
文件夹
CalculatorTest.java
- &gt; src/test
文件夹
答案 2 :(得分:0)
由于您的课程位于不同的目录中,
导入Calculator
课程中的CalculatorTest
课程,然后重试。
希望这有帮助。
答案 3 :(得分:0)
总结到@javaguy的答案,确保包是相同的。如果要使用不带导入语句的Calculator类,则需要在同一个包中同时使用Calculator和CalculatorTest类。
答案 4 :(得分:0)
主类应位于src / main / java
下
和
测试类应在src / test / java
如果无法访问所有正确的位置和仍然是主类,则
右键单击项目=> Maven =>更新项目
希望这样可以解决问题