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

发布于 2025-01-17 04:24:43 字数 1205 浏览 0 评论 0原文

我正在尝试使用尽可能少的设置来使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2025-01-24 04:24:43

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

按照以下步骤操作:

  1. 下载 JUnit Console Launcher jar
  2. 使用刚刚下载的 jar 编译测试所需的类
  3. 执行指定要测试的类的测试(有很多关于如何选择类和方法的选项)

运行的命令:

$ 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

我 类更正

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$1.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          ]

First of all, my Java was wrong. Second I wasn't using the right commands.

Following the steps:

  1. Download JUnit Console Launcher jar
  2. Compile class needed for the test with the jar you just downloaded
  3. Execute test specifying the class you want to test(there a lot of option on how to chose classes and methods)

The commands I ran:

$ 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

The classes corrected:

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;
    }
}

Result of the tests:

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$1.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          ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文