2

I am writing my first junit test class. My calculator.java class is in my src directory and my test class (CalculatorTest.java) is in test/src directory. My problem is that CalculatorTest class does not recognize the calculator class. Any thoughts? Here it is my calculator.java class in src directory:

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;
}
}

And it is my CalculatorTest class in test/src directory:

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); 
}
}

This is my 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>

This is the error:

cannot find symbol Calculator cut = new Calculator();

Thanks

2
  • maybe your classpath exist flaw.
    – nail fei
    Commented Nov 19, 2016 at 7:16
  • Are your both classes in different package ? Commented Nov 19, 2016 at 7:21

5 Answers 5

2

My calculator.java class is in my src direcotry and my test class (CalculatorTest.java) is in test/src directory. My problem is that CalculatorTest class does not recognize the calculator class. Any thoughts?

Your folder structure seems incorrect and you need to have the folder structure as shown below:

Calculator.java -> src/main folder

CalculatorTest.java -> src/test folder

1
  • I posted my build.xml file.. I suspect the problem is in this file.. could you look at it? thanks
    – Hamid_UMB
    Commented Nov 19, 2016 at 18:20
1

You will need to import the class Calculator

Usually the application classes are under source or src/main folder, whereas the test classes reside in src/test directory.

So you will have to make sure that the Calculator class is imported in your test class i.e. CalculatorTest.

1
  • I want to have my test classes in separate folder (test folder) not in the src/test folder.
    – Hamid_UMB
    Commented Nov 19, 2016 at 17:50
0

Since your classes are in different directories,

Import the Calculator class in your CalculatorTest class and try it again.

Hope this helps.

1
  • Thanks... I imported the Calculator class and Also I am using same package names.. but still does not work.
    – Hamid_UMB
    Commented Nov 19, 2016 at 17:04
0

Summed to @javaguy answer, make sure that the packages are the same. If you want to use the Calculator class without the import sentence, you need to have both classes, Calculator and CalculatorTest in the same package.

1
  • I edited my answer using your suggestion. But still no luck
    – Hamid_UMB
    Commented Nov 19, 2016 at 17:05
0

Main classes should be under src/main/java
and
test classes should be under src/test/java

If all in the correct places and still main classes are not accessible then
Right click project => Maven => Update Project
Hope so this will resolve the issue

Not the answer you're looking for? Browse other questions tagged or ask your own question.