3

I have a simple problem I don't know how to solve!

I have a single Java file User.java:

import java.util.Vector;

public class User {
    private String name;
    private Vector<User> friends;

    public User(String name) {
        this.name = name;
        this.friends = new Vector<>();
    }

    public void addFriend(User newfriend) {
        friends.add(newfriend);
    }

    public boolean isFriendsWith(User friend) {
        return friends.indexOf(friend) != -1;
    }
}

and I have a simple test class UserTest.java beside this class:

import static org.junit.Assert.assertEquals;
import org.junit.Test;


public class UserTest {
    @Test
    public void evaluatesExpression() {
        User user = new User("foo");
        User user2 = new User("bar");
        user.addFriend(user2);
        assertEquals(true, user.isFriendsWith(user2));
    }
}

I want to run this test class for the User class. I'm not using IDEs like IntelliJ or Eclipse, so I want to compile the test from linux command line, but this command:

javac -cp .:"/usr/share/java/junit.jar" UserTest.java

gives me the following errors:

UserTest.java:1: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
                       ^
UserTest.java:1: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
UserTest.java:2: error: package org.junit does not exist
import org.junit.Test;
                ^
UserTest.java:6: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class UserTest
UserTest.java:11: error: cannot find symbol
       assertEquals(true, user.isFriendsWith(user2));
        ^
  symbol:   method assertEquals(boolean,boolean)
  location: class UserTest
5 errors

Note: everything I have seen on Stackoverflow is about testing a single file in a project or building and testing with gradle and ..., but I don't know much about Java and I don't need to know much, I just need to know the simplest way to create and run a test for a single Java class.

Note2: I have installed junit with apt install junit and it installed junit-3-8-2 version.

Note3: I have problems when trying to compile my test class, I haven't even reached the stage where I can run the tests!

ganjim
  • 1,234
  • 1
  • 16
  • 30
  • Off-topic, but never use `Vector`. – shmosel Oct 09 '18 at 20:26
  • @shmosel thanks, but its a part of the code given to me for a project! – ganjim Oct 09 '18 at 20:28
  • 2
    Possible duplicate of [How to run JUnit test cases from the command line](https://stackoverflow.com/questions/2235276/how-to-run-junit-test-cases-from-the-command-line) – PM 77-1 Oct 09 '18 at 20:29
  • @PM77-1 the answers are for running the test, but I think my problem is in building the test class! – ganjim Oct 09 '18 at 20:32
  • 1
    `javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/TestClassName.java` as described in the answer mentioned above. – Andreas Oct 09 '18 at 20:37
  • From Java perspective "*test class*" is no different than any other. You just need to have external JARs somewhere on class path. – PM 77-1 Oct 09 '18 at 20:41
  • @Andreas I tried this and it still gives me the same errors – ganjim Oct 09 '18 at 20:42
  • @PM77-1 external JARs of what?? if you mean like `junit.jar` you can see that unfortunately it does not work! – ganjim Oct 09 '18 at 20:43
  • No, I cannot see it. Where is it located? – PM 77-1 Oct 09 '18 at 20:46
  • I'm sorry I must have written the command wrong, I edited the command I used to compile the class – ganjim Oct 09 '18 at 20:47
  • java -cp [file path + name of your junit-3-8-2] junit.textui.TestRunner UserTest.java – wenzi Oct 09 '18 at 20:47
  • @wenzi I thinks thats for running the compiled test! my test doesn't compile – ganjim Oct 09 '18 at 20:48
  • Is your Java version 9 or above? – PM 77-1 Oct 09 '18 at 20:49
  • @PM77-1 yes `java --version` shows `openjdk 10.0.2 2018-07-17` I also installed java with `apt` I thought these where the defaults. – ganjim Oct 09 '18 at 20:50
  • 3
    `junit-3-8-2` is way to old. It does not use the org.junit namesapce afair. Use at least 4.x.. The root cause is, that there is no `org.junit.Assert` on your classpath, because the libs you are using are before the JUnit team did a rename. But the test you are trying to run uses the 4.x code. https://mvnrepository.com/artifact/junit/junit/4.12 – Andreas Oct 09 '18 at 20:52
  • junit.jar are you sure that is the file name? – wenzi Oct 09 '18 at 20:52
  • there are two files there that appear to be about junit: `junit.jar` and `junit-3.8.2.jar` and I have tested the above script with both of them. – ganjim Oct 09 '18 at 20:53
  • try removing the quotes? – wenzi Oct 09 '18 at 20:57
  • @wenzi still doesn't work – ganjim Oct 09 '18 at 20:58
  • javac -cp ".:/usr/share/java/junit-3.8.2.jar" UserTest.java – wenzi Oct 09 '18 at 20:58
  • @Andreas explained your problem. Did you pay attention? – PM 77-1 Oct 09 '18 at 20:58
  • @wenzi that doesn't work either – ganjim Oct 09 '18 at 20:59
  • @PM77-1 thanks! I missed it, there are too much comments in here! – ganjim Oct 09 '18 at 20:59
  • @Andreas Thanks, using junit4 solved my problem, I will set this as the correct answer if you post it as an answer. – ganjim Oct 09 '18 at 21:04
  • @Mohammad Ganji sorry for kind of distracting you from the real solution, glad that you find the solution – wenzi Oct 10 '18 at 00:32

1 Answers1

2

After quite a lot of trial and error in the comments section, the root cause was an old JUnit 3.8.2 dependency. The 3.x releases used a different namespace that was changed in 4.x to org.junit.

Therefore the classes where not found while compiling the test.

To debug such issues unzipping the jar with unzip on Linux can be helpful.

Andreas
  • 5,251
  • 30
  • 43
  • 1
    On windows rename the .jar to .zip and open it.. Should work without any other software installed. We use this technique to examine wars quite a bit (which are also just zip files) – Bill K Oct 09 '18 at 21:15