如何在不使用 Maven(Gradle 或任何构建自动化工具)的情况下使用 JUnit?
我正在尝试使用尽可能少的设置来使用 JUnit Console Launcher 为我的 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 ]
我预计会有一个坠落测试。但我的测试没有被认可。
I'm trying to use as little setup as possible to create an ambient for unit tests for my java code using JUnit Console Launcher.
Following the classes created for testing:
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;
}
}
Command:
$ java -jar junit-platform-console-standalone-1.8.2.jar -o SumTest.java
Output:
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 ]
I expected to have a falling test. But my test wasn't recognized.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我的Java是错误的。其次,我没有使用正确的命令。
按照以下步骤操作:
运行的命令:
我 类更正:
SumTest.java
Sum.java
测试结果:
First of all, my Java was wrong. Second I wasn't using the right commands.
Following the steps:
The commands I ran:
The classes corrected:
SumTest.java
Sum.java
Result of the tests: