如何从终端使用junit [英] how to use junit from the terminal

查看:93
本文介绍了如何从终端使用junit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在终端中使用junit. 我有一个Abc.java类

I am trying to use junit in the terminal. I have a class Abc.java

class Abc{
    public int add(int a, int b){
         return a+b
    }
}

我创建了一个类AbcTest.java

and i have created a class AbcTest.java

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

class AbcTest {
    public void testAdd() {
        System.out.println("add");
        int a = 0;
        int b = 0;
        Abc instance = new Abc();
        int expResult = 0;
        int result = instance.add(a, b);
        assertEquals(expResult, result);
    }
}

当我运行命令时

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

我收到以下错误输出

AbcTest.java:16: error: cannot find symbol
Abc instance = new Abc();
^
symbol:   class Abc
location: class AbcTest
AbcTest.java:16: error: cannot find symbol
Abc instance = new Abc();
                   ^
symbol:   class Abc
location: class AbcTest
2 errors

我尝试使用命令

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

它正确建立,但是运行此命令

it build up properly, but running this command

java -cp /usr/share/java/junit4.jar org.junit.runner.JUnitCore AbcTest

引发以下错误

JUnit version 4.11
Could not find class: AbcTest
Time: 0.002
OK (0 tests)

推荐答案

您需要将当前目录(或编译类文件所在的目录)以及必要的jar文件添加到类路径.

You need to add current directory (or the directory where the compile class file are) along with the necessary jar files to class path.

java -cp /usr/share/java/junit4.jar:. org.junit.runner.JUnitCore AbcTest

仅供参考,当我尝试运行相同的测试用例时(当前目录中的jar文件和类文件)

FYI When I tried to run the same test case I used (jar files and class files in current directory)

javac -cp junit-4.11.jar:. AbcTest.java
java -cp junit-4.11.jar:hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore AbcTest

并且不得不将AbcTest.java修改为

And had to modify the AbcTest.java as

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;


public class AbcTest {
    @Test
    public void testAdd() {
        System.out.println("add");
        int a = 0;
        int b = 0;
        Abc instance = new Abc();
        int expResult = 0;
        int result = instance.add(a, b);
        assertEquals(expResult, result);
    }
}

更改:

  1. 公共类AbcTest
  2. @TestAdd方法的测试注释

这篇关于如何从终端使用junit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆