Issue
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.
Solution
First of all, my Java was wrong. Second I wasn't using the right commands.
Following the steps:
- Download JUnit Console Launcher jar
- Compile class needed for the test with the jar you just downloaded
- 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 ]
Answered By - João Pedro Gonçalves
Answer Checked By - Marilyn (JavaFixing Volunteer)