junit5 - What's the equivalent of org.junit.runner.JUnitCore.runClasses in Junit 5? -
the following code started in junit4 , has been translated junit5 except main()
. reason i'm writing way i'm demonstrating tdd , have multiple versions of stringinverter
implementation, each of implements more features , passes more tests. here stringinverter
interface:
interface stringinverter { public string invert(string str); }
and here's almost-compiling-with-junit5 class:
import java.util.*; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.assertions.*; import org.junit.platform.runner.junitplatform; public class stringinvertertest { static stringinverter inverter; @test public final void basicinversion_succeed() { string in = "exit, pursued bear."; string out = "exit, pursued bear."; assertequals(inverter.invert(in), out); } @test public final void basicinversion_fail() { expectthrows(runtimeexception.class, () -> { assertequals(inverter.invert("x"), "x"); }); } @test public final void allowedcharacters_fail() { expectthrows(runtimeexception.class, () -> { inverter.invert(";-_()*&^%$#@!~`"); inverter.invert("0123456789"); }); } @test public final void allowedcharacters_succeed() { inverter.invert("abcdefghijklmnopqrstuvwxyz ,."); inverter.invert("abcdefghijklmnopqrstuvwxyz ,."); } @test public final void lengthlessthan26_fail() { string str = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; asserttrue(str.length() > 25); expectthrows(runtimeexception.class, () -> { inverter.invert(str); }); } @test public final void lengthlessthan26_succeed() { string str = "xxxxxxxxxxxxxxxxxxxxxxxxx"; asserttrue(str.length() < 26); inverter.invert(str); } public static void main(string[] args) throws exception{ assertequals(args.length, 1); inverter = (stringinverter) class.forname(args[0]).newinstance(); result result = org.junit.runner.junitcore.runclasses( stringinvertertest.class); list<failure> failures = result.getfailures(); system.out.printf("%s has %d failures:\n", args[0], failures.size()); int count = 1; for(failure f : failures) { system.out.printf("failure %d:\n", count++); system.out.println(f.getdescription()); system.out.println(f.getmessage()); } } }
main()
worked junit4, question how convert junit 5. thanks!
junit5 has launcher api in junit-platform-launcher module programmatic test discovery , execution.
detailed example documented on user guide chapter7.
Comments
Post a Comment