0

I have a class called Calculator, the code is as below and it can be compiled successfully.

public class Calculator {

    private static int result;

    public void add(int n) {
        result = result + n;
    }

    public void substance(int n) {
        result = result - 1; // just a bug for some purpose
    }

    ... .... // some other methods
}

I have another class called CalculatorTest.java using Junit4 to test Calculator, whose code is as

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {

    private static Calculator ca = new Calculator();
    // initialise
    @Before
    public void setUp() throws Exception {
        ca.clear();//  clear the result
    }

    @Test
    public void testAdd() {
        // fail("Not yet implemented");
        ca.add(2);
        ca.add(3);
        assertEquals(5, ca.getResult());
    }

    @Test
    public void testSubstance() {
        // fail("Not yet implemented");
        ca.add(10);
        ca.substance(5);
        assertEquals(5, ca.getResult());
    }
    ... .... // some other methods
}

In the terminal, I run:

javac -cp /usr/share/java/junit4.jar CalculatorTest.java

The result is :

CalculatorTest.java:12: error: cannot find symbol
    private static Calculator ca = new Calculator();
                   ^
  symbol:   class Calculator
  location: class CalculatorTest
CalculatorTest.java:12: error: cannot find symbol
    private static Calculator ca = new Calculator();
                                       ^
  symbol:   class Calculator
  location: class CalculatorTest
2 errors

So, what the problem can be?

The spelling is correct and Calculator is not a java keyword.

Thank you.

Justin
  • 327
  • 3
  • 13
  • Is test package the same as src package? – PatrickChen Jun 05 '20 at 01:51
  • 2
    `javac -cp /usr/share/java/junit4.jar;. CalculatorTest.java` on Windows, `javac -cp /usr/share/java/junit4.jar:. CalculatorTest.java` everywhere else. – Elliott Frisch Jun 05 '20 at 01:51
  • @ElliottFrisch, thank you. It worked. A new question comes up when I run: java CalculatorTest, it says, I don't have the static void main. But many JUnit test examples don't have it either. So, how I can solve it? Thank you. – Justin Jun 05 '20 at 02:02
  • [`TestRunner`](http://junit.sourceforge.net/junit3.8.1/javadoc/junit/textui/TestRunner.html) is *A command line based tool to run tests. `java junit.textui.TestRunner [-wait] TestCaseClass`* – Elliott Frisch Jun 05 '20 at 02:14
  • @ElliottFrisch, I did: `java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner CalculatorTest`, warning says: `1) warning(junit.framework.TestSuite$1)junit.framework.AssertionFailedError: No tests found in CalculatorTest`. I tried methods from https://stackoverflow.com/questions/2235276/how-to-run-junit-test-cases-from-the-command-line, still there are warnings. I have these `junit-3.8.2.jar, junit4-4.12.jar, junit4.jar, junit.jar` . – Justin Jun 05 '20 at 02:36
  • What platform are you on? You want `java -cp /usr/share/java/junit4.jar;. junit.textui.TestRunner CalculatorTest` on Windows. Use `:` if you're not on Windows. And make sure you use the same version of `junit` that you compiled with. – Elliott Frisch Jun 05 '20 at 02:43
  • @ElliottFrisch, I'm using Ubuntu and typied: `java -cp /usr/share/java/junit4.jar:. junit.textui.TestRunner CalculatorTest`. At this point, the warning is still there. Anything else I could check? – Justin Jun 05 '20 at 03:02
  • try importing Calculator class in CalculatorTest – sarwadnya deshpande Jun 05 '20 at 03:49
  • @sarwadnyadeshpande, `Calculator` and `CalculatorTest` are under the same path. – Justin Jun 05 '20 at 04:00
  • @ElliottFrisch, thank you for helping me and I just fixed it by using JUnit3 style, i.e. `import junit.framework.TestCase` and `public class CalculatorTest2 extends TestCase{ ... ..}` . Phrase `extends TestCase` is added. But still I typied `java -cp .:/usr/share/java/junit4.jar junit.textui.TestRunner CalculatorTest`, where `junit4` is used. – Justin Jun 05 '20 at 04:23
  • I've been using TestNG for a few years now, so I couldn't really offer any more advice beyond what I remembered and read in your question. – Elliott Frisch Jun 05 '20 at 04:33

0 Answers0