在Eclipse和终端/命令提示符中运行JUnit

时间:2019-10-12 22:52:31

标签: java eclipse junit terminal command-prompt

说明-

  • 我有一个Java文件(Service.java),其中包含多个方法(higher(int,int):intisPerfectSquare(int):boolean)。
  • 我在ServiceTest.java中为此类创建了一个包含testHigher()testIsPerfectSquare()的测试。
  • 如果两种方法之一(在我的示例中为higher中)发生编译错误,Eclipse仍将运行测试,并显示一次通过和一个错误。
  • 但是,使用以下命令在终端中编译会导致错误。

     javac -cp out:junit-platform-console-standalone-1.5.2.jar Service.java ServiceTest.java
    
  • 这意味着,我无法使用以下命令在终端中执行

     java -jar junit-platform-console-standalone-1.5.2.jar -class-path . --scan-class-path
    

问题-

即使在被测类的某些方法中出现编译错误,如何才能继续执行测试(就像Eclipse一样)。任何帮助,将不胜感激。谢谢!

下面的相关代码-

public class Service {
    public static int higher(int a, int b) { //has compilation error
        if(a > b) {
            return a; 
        }
        if(b > a) {
            return b;
        }
    }

    public static boolean isPerfectSquare(int n) {
        return Math.pow((int)Math.sqrt(n), 2) == n;
    }
}
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class ServiceTest {

    @Test
    void testHigher() {
        assertEquals(5, Service.higher(3, 5));
        assertEquals(6, Service.higher(6, 4));
        assertEquals(7, Service.higher(7, 7));
    }

    @Test
    void testIsPerfectSquare() {
        assertTrue(Service.isPerfectSquare(64));
        assertFalse(Service.isPerfectSquare(63));
    }
}

0 个答案:

没有答案
相关问题