如何在不使用 Maven(Gradle 或任何构建自动化工具)的情况下使用 JUnit?

How to use JUnit without using Maven(Graddle or any build automation tool)?

我正在尝试使用尽可能少的设置为使用 JUnit 控制台启动器的 java 代码创建单元测试环境。

在为测试创建的 类 之后:

SumTest.java

import static org.junit.jupiter.api.Assertions.assertEquals;
    
public class SumTest {
    
    @Test
    public void sumTest() {
        assertEquals(10, sum(1,0));        
    }
}

Sum.java

public class Sum {
    public int sum(int a, int b) {
        return a + b;
    }
}  

命令:

 $ java -jar junit-platform-console-standalone-1.8.2.jar -o SumTest.java

输出:

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔

Test run finished after 88 ms
[         2 containers found      ]
[         0 containers skipped    ]
[         2 containers started    ]
[         0 containers aborted    ]
[         2 containers successful ]
[         0 containers failed     ]
[         0 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]

我预计会进行坠落测试。但是我的测试没有被识别。

首先,我的Java是错误的。其次我没有使用正确的命令。

执行以下步骤:

  1. 下载 JUnit 控制台启动器 jar
  2. 用刚下载的jar编译测试需要的class
  3. 执行指定要测试的 class 的测试(关于如何选择 classes 和方法有很多选项)

命令我运行:

$ Var=junit-platform-console-standalone-1.8.2.jar
$ curl https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.8.2/junit-platform-console-standalone-1.8.2.jar --output $Var
$ javac -cp "junit-platform-console-standalone-1.8.2.jar" Sum.java SumTest.java
$ java -jar junit-platform-console-standalone-1.8.2.jar -cp "." --select-class SumTest

classes 更正:

SumTest.java

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

public class SumTest {
    @Test
    public void sumTest() {
        assertEquals(10, Sum.sum(5, 5));
    }

    @Test
    public void sumTest2() {
        assertEquals(10, Sum.sum(4, 5));
    }
}

Sum.java

public class Sum {
    static int sum(int a, int b) {
        return a + b;
    }
}

测试结果:

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔
   └─ SumTest ✔
      ├─ sumTest ✔
      └─ sumTest2 ✘ expected:<10> but was:<9>

Failures (1):
  JUnit Vintage:SumTest:sumTest2
    MethodSource [className = 'SumTest', methodName = 'sumTest2', methodParameterTypes = '']
    => java.lang.AssertionError: expected:<10> but was:<9>
       org.junit.Assert.fail(Assert.java:89)
       org.junit.Assert.failNotEquals(Assert.java:835)
       org.junit.Assert.assertEquals(Assert.java:647)
       org.junit.Assert.assertEquals(Assert.java:633)
       SumTest.sumTest2(SumTest.java:12)
       java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
       java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       java.base/java.lang.reflect.Method.invoke(Method.java:568)
       org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
       [...]

Test run finished after 135 ms
[         3 containers found      ]
[         0 containers skipped    ]
[         3 containers started    ]
[         0 containers aborted    ]
[         3 containers successful ]
[         0 containers failed     ]
[         2 tests found           ]
[         0 tests skipped         ]
[         2 tests started         ]
[         0 tests aborted         ]
[         1 tests successful      ]
[         1 tests failed          ]