简体   繁体   English

在单个Java类的命令行中使用JUnit编译并运行测试

[英]Compile and run test with JUnit in command line on a single Java class

I have a simple problem I don't know how to solve! 我有一个简单的问题,我不知道如何解决!

I have a single Java file User.java : 我有一个Java文件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: 我在这个类旁边有一个简单的测试类UserTest.java

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. 我想为User类运行此测试类。 I'm not using IDEs like IntelliJ or Eclipse, so I want to compile the test from linux command line, but this command: 我没有使用像IntelliJ或Eclipse这样的IDE,所以我想从linux命令行编译测试,但是这个命令:

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. 注意:我在Stackoverflow上看到的所有内容都是关于在项目中测试单个文件或使用gradle和...进行构建和测试,但我对Java知之甚少,我不需要了解太多,我只需要了解为单个Java类创建和运行测试的最简单方法。

Note2: I have installed junit with apt install junit and it installed junit-3-8-2 version. 注2:我已经安装了junit和apt install junit ,并安装了junit-3-8-2版本。

Note3: I have problems when trying to compile my test class, I haven't even reached the stage where I can run the tests! 注3:我在尝试编译测试类时遇到问题,我甚至没有达到可以运行测试的阶段!

After quite a lot of trial and error in the comments section, the root cause was an old JUnit 3.8.2 dependency. 在评论部分进行了大量的试验和错误后,根本原因是旧的JUnit 3.8.2依赖。 The 3.x releases used a different namespace that was changed in 4.x to org.junit . 3.x版本使用了在4.x中更改为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. 要调试这些问题,在Linux上解压缩jar unzip可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM