How to use evaluate method of org.junit.runners.model.Statement class

Best junit code snippet using org.junit.runners.model.Statement.evaluate

Source:TailoredBenchmarkFactoryTest.java Github

copy

Full Screen

...95 " private ClassRuleTest instance;",96 "",97 " @org.openjdk.jmh.annotations.Benchmark",98 " public void benchmark_test() throws java.lang.Throwable {",99 " this.payloads.test.evaluate();",100 " }",101 "",102 " private static class _ClassStatement",103 " extends org.junit.runners.model.Statement {",104 " private final se.chalmers.ju2jmh.api.ThrowingConsumer<ClassRuleTest> payload;",105 " private final _Benchmark benchmark;",106 " private final org.junit.runner.Description description;",107 "",108 " private _ClassStatement(",109 " se.chalmers.ju2jmh.api.ThrowingConsumer<ClassRuleTest> payload,",110 " _Benchmark benchmark,",111 " org.junit.runner.Description description) {",112 " this.payload = payload;",113 " this.benchmark = benchmark;",114 " this.description = description;",115 " }",116 "",117 " @java.lang.Override",118 " public void evaluate() throws java.lang.Throwable {",119 " this.benchmark.instance = new ClassRuleTest();",120 " this.payload.accept(this.benchmark.instance);",121 " }",122 "",123 " public static org.junit.runners.model.Statement forPayload(",124 " se.chalmers.ju2jmh.api.ThrowingConsumer<ClassRuleTest> payload,",125 " String name,",126 " _Benchmark benchmark) {",127 " org.junit.runner.Description description =",128 " se.chalmers.ju2jmh.api.Rules.description(ClassRuleTest.class, name);",129 " org.junit.runners.model.Statement statement =",130 " new _ClassStatement(payload, benchmark, description);",131 " statement = se.chalmers.ju2jmh.api.Rules.apply(",132 " ClassRuleTest.rule, statement, description);",133 " return statement;",134 " }",135 " }",136 "",137 " private static class _Payloads {",138 " public org.junit.runners.model.Statement test;",139 " }",140 "",141 " @org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",142 " public void makePayloads() {",143 " this.payloads = new _Payloads();",144 " this.payloads.test =",145 " _ClassStatement.forPayload(ClassRuleTest::test, \"test\", this);",146 " }",147 "}"148 );149 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.ClassRuleTest")150 .withTest("test")151 .withClassRuleField("rule")152 .build();153 ClassOrInterfaceDeclaration benchmark =154 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);155 assertThat(benchmark, equalsAst(expected));156 }157 @Test158 public void handlesInstanceRule() {159 ClassOrInterfaceDeclaration expected = classFromLines(160 "@org.openjdk.jmh.annotations.State(org.openjdk.jmh.annotations.Scope.Thread)",161 "public static class _Benchmark {",162 " private _Payloads payloads;",163 " private InstanceRuleTest instance;",164 "",165 " @org.openjdk.jmh.annotations.Benchmark",166 " public void benchmark_test() throws java.lang.Throwable {",167 " this.payloads.test.evaluate();",168 " }",169 "",170 " private static class _InstanceStatement",171 " extends org.junit.runners.model.Statement {",172 " private final se.chalmers.ju2jmh.api.ThrowingConsumer<InstanceRuleTest>",173 " payload;",174 " private final _Benchmark benchmark;",175 "",176 " public _InstanceStatement(",177 " se.chalmers.ju2jmh.api.ThrowingConsumer<InstanceRuleTest> payload,",178 " _Benchmark benchmark) {",179 " this.payload = payload;",180 " this.benchmark = benchmark;",181 " }",182 "",183 " @java.lang.Override",184 " public void evaluate() throws java.lang.Throwable {",185 " this.payload.accept(this.benchmark.instance);",186 " }",187 " }",188 "",189 " private static class _ClassStatement",190 " extends org.junit.runners.model.Statement {",191 " private final se.chalmers.ju2jmh.api.ThrowingConsumer<InstanceRuleTest>",192 " payload;",193 " private final _Benchmark benchmark;",194 " private final org.junit.runner.Description description;",195 " private final org.junit.runners.model.FrameworkMethod frameworkMethod;",196 "",197 " private _ClassStatement(",198 " se.chalmers.ju2jmh.api.ThrowingConsumer<InstanceRuleTest> payload,",199 " _Benchmark benchmark,",200 " org.junit.runner.Description description,",201 " org.junit.runners.model.FrameworkMethod frameworkMethod) {",202 " this.payload = payload;",203 " this.benchmark = benchmark;",204 " this.description = description;",205 " this.frameworkMethod = frameworkMethod;",206 " }",207 "",208 " @java.lang.Override",209 " public void evaluate() throws java.lang.Throwable {",210 " this.benchmark.instance = new InstanceRuleTest();",211 " org.junit.runners.model.Statement statement =",212 " new _InstanceStatement(this.payload, this.benchmark);",213 " statement = this.applyRule(this.benchmark.instance.rule, statement);",214 " statement.evaluate();",215 " }",216 "",217 " private org.junit.runners.model.Statement applyRule(",218 " org.junit.rules.TestRule rule,",219 " org.junit.runners.model.Statement statement) {",220 " return se.chalmers.ju2jmh.api.Rules.apply(",221 " rule, statement, this.description);",222 " }",223 "",224 " private org.junit.runners.model.Statement applyRule(",225 " org.junit.rules.MethodRule rule,",226 " org.junit.runners.model.Statement statement) {",227 " return se.chalmers.ju2jmh.api.Rules.apply(",228 " rule, statement, this.frameworkMethod, this.benchmark.instance);",229 " }",230 "",231 " public static org.junit.runners.model.Statement forPayload(",232 " se.chalmers.ju2jmh.api.ThrowingConsumer<InstanceRuleTest> payload,",233 " String name,",234 " _Benchmark benchmark) {",235 " org.junit.runner.Description description =",236 " se.chalmers.ju2jmh.api.Rules.description(InstanceRuleTest.class, name);",237 " org.junit.runners.model.FrameworkMethod frameworkMethod =",238 " se.chalmers.ju2jmh.api.Rules.frameworkMethod(",239 " InstanceRuleTest.class, name);",240 " org.junit.runners.model.Statement statement =",241 " new _ClassStatement(payload, benchmark, description, frameworkMethod);",242 " return statement;",243 " }",244 " }",245 "",246 " private static class _Payloads {",247 " public org.junit.runners.model.Statement test;",248 " }",249 "",250 " @org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",251 " public void makePayloads() {",252 " this.payloads = new _Payloads();",253 " this.payloads.test =",254 " _ClassStatement.forPayload(InstanceRuleTest::test, \"test\", this);",255 " }",256 "}"257 );258 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.InstanceRuleTest")259 .withTest("test")260 .withInstanceRuleField("rule")261 .build();262 ClassOrInterfaceDeclaration benchmark =263 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);264 assertThat(benchmark, equalsAst(expected));265 }266 @Test267 public void handlesMultipleTestsWithoutRules() {268 MethodDeclaration test1Expected = methodFromLines(269 "@org.openjdk.jmh.annotations.Benchmark",270 "public void benchmark_test1() throws java.lang.Throwable {",271 " this.runBenchmark(this.payloads.test1);",272 "}");273 MethodDeclaration test2Expected = methodFromLines(274 "@org.openjdk.jmh.annotations.Benchmark",275 "public void benchmark_test2() throws java.lang.Throwable {",276 " this.runBenchmark(this.payloads.test2);",277 "}");278 ClassOrInterfaceDeclaration payloadsExpected = classFromLines(279 "private static class _Payloads {",280 " public se.chalmers.ju2jmh.api.ThrowingConsumer<Test> test1;",281 " public se.chalmers.ju2jmh.api.ThrowingConsumer<Test> test2;",282 "}");283 MethodDeclaration makePayloadsExpected = methodFromLines(284 "@org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",285 "public void makePayloads() {",286 " this.payloads = new _Payloads();",287 " this.payloads.test1 = Test::test1;",288 " this.payloads.test2 = Test::test2;",289 "}");290 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")291 .withTest("test1")292 .withTest("test2")293 .build();294 ClassOrInterfaceDeclaration benchmark =295 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);296 MethodDeclaration test1 = getMethod(benchmark, "benchmark_test1");297 MethodDeclaration test2 = getMethod(benchmark, "benchmark_test2");298 ClassOrInterfaceDeclaration payloads = getNestedClass(benchmark, "_Payloads");299 MethodDeclaration makePayloads = getMethod(benchmark, "makePayloads");300 assertThat(test1, equalsAst(test1Expected));301 assertThat(test2, equalsAst(test2Expected));302 assertThat(payloads, equalsAst(payloadsExpected));303 assertThat(makePayloads, equalsAst(makePayloadsExpected));304 }305 @Test306 public void handlesMultipleTestsWithRules() {307 MethodDeclaration test1Expected = methodFromLines(308 "@org.openjdk.jmh.annotations.Benchmark",309 "public void benchmark_test1() throws java.lang.Throwable {",310 " this.payloads.test1.evaluate();",311 "}");312 MethodDeclaration test2Expected = methodFromLines(313 "@org.openjdk.jmh.annotations.Benchmark",314 "public void benchmark_test2() throws java.lang.Throwable {",315 " this.payloads.test2.evaluate();",316 "}");317 ClassOrInterfaceDeclaration payloadsExpected = classFromLines(318 "private static class _Payloads {",319 " public org.junit.runners.model.Statement test1;",320 " public org.junit.runners.model.Statement test2;",321 "}");322 MethodDeclaration makePayloadsExpected = methodFromLines(323 "@org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",324 "public void makePayloads() {",325 " this.payloads = new _Payloads();",326 " this.payloads.test1 = _ClassStatement.forPayload(Test::test1, \"test1\", this);",327 " this.payloads.test2 = _ClassStatement.forPayload(Test::test2, \"test2\", this);",328 "}");329 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")330 .withTest("test1")331 .withTest("test2")332 .withClassRuleField("rule")333 .build();334 ClassOrInterfaceDeclaration benchmark =335 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);336 MethodDeclaration test1 = getMethod(benchmark, "benchmark_test1");337 MethodDeclaration test2 = getMethod(benchmark, "benchmark_test2");338 ClassOrInterfaceDeclaration payloads = getNestedClass(benchmark, "_Payloads");339 MethodDeclaration makePayloads = getMethod(benchmark, "makePayloads");340 assertThat(test1, equalsAst(test1Expected));341 assertThat(test2, equalsAst(test2Expected));342 assertThat(payloads, equalsAst(payloadsExpected));343 assertThat(makePayloads, equalsAst(makePayloadsExpected));344 }345 @Test346 public void handlesExceptionTestsWithoutRules() {347 MethodDeclaration makePayloadsExpected = methodFromLines(348 "@org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",349 "public void makePayloads() {",350 " this.payloads = new _Payloads();",351 " this.payloads.exceptionTest = new se.chalmers.ju2jmh.api.ExceptionTest<>(",352 " Test::exceptionTest, java.lang.Exception.class);",353 "}");354 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")355 .withExceptionTest("exceptionTest", "java.lang.Exception")356 .build();357 ClassOrInterfaceDeclaration benchmark =358 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);359 MethodDeclaration makePayloads = getMethod(benchmark, "makePayloads");360 assertThat(makePayloads, equalsAst(makePayloadsExpected));361 }362 @Test363 public void handlesExceptionTestsWithRules() {364 MethodDeclaration makePayloadsExpected = methodFromLines(365 "@org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",366 "public void makePayloads() {",367 " this.payloads = new _Payloads();",368 " this.payloads.exceptionTest = _ClassStatement.forPayload(",369 " new se.chalmers.ju2jmh.api.ExceptionTest<>(",370 " Test::exceptionTest, java.lang.Exception.class),",371 " \"exceptionTest\", this);",372 "}");373 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")374 .withExceptionTest("exceptionTest", "java.lang.Exception")375 .withClassRuleField("rule")376 .build();377 ClassOrInterfaceDeclaration benchmark =378 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);379 MethodDeclaration makePayloads = getMethod(benchmark, "makePayloads");380 assertThat(makePayloads, equalsAst(makePayloadsExpected));381 }382 @Test383 public void handlesBeforeWithoutRules() {384 BlockStmt expectedRunBenchmarkBody = blockFromLines(385 "{",386 " this.instance = new Test();",387 " this.instance.before1();",388 " this.instance.before2();",389 " payload.accept(this.instance);",390 "}");391 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")392 .withBefore("before1")393 .withBefore("before2")394 .build();395 ClassOrInterfaceDeclaration benchmark =396 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);397 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");398 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));399 }400 @Test401 public void handlesAfterWithoutRules() {402 BlockStmt expectedRunBenchmarkBody = blockFromLines(403 "{",404 " this.instance = new Test();",405 " try {",406 " payload.accept(this.instance);",407 " } finally {",408 " this.instance.after1();",409 " this.instance.after2();",410 " }",411 "}");412 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")413 .withAfter("after1")414 .withAfter("after2")415 .build();416 ClassOrInterfaceDeclaration benchmark =417 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);418 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");419 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));420 }421 @Test422 public void handlesBeforeClassWithoutRules() {423 BlockStmt expectedRunBenchmarkBody = blockFromLines(424 "{",425 " Test.beforeClass1();",426 " Test.beforeClass2();",427 " this.instance = new Test();",428 " payload.accept(this.instance);",429 "}");430 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")431 .withBeforeClass("beforeClass1")432 .withBeforeClass("beforeClass2")433 .build();434 ClassOrInterfaceDeclaration benchmark =435 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);436 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");437 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));438 }439 @Test440 public void handlesAfterClassWithoutRules() {441 BlockStmt expectedRunBenchmarkBody = blockFromLines(442 "{",443 " try {",444 " this.instance = new Test();",445 " payload.accept(this.instance);",446 " } finally {",447 " Test.afterClass1();",448 " Test.afterClass2();",449 " }",450 "}");451 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")452 .withTest("test")453 .withAfterClass("afterClass1")454 .withAfterClass("afterClass2")455 .build();456 ClassOrInterfaceDeclaration benchmark =457 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);458 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");459 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));460 }461 @Test462 public void handlesMultipleFixtureMethodsWithoutRules() {463 BlockStmt expectedRunBenchmarkBody = blockFromLines(464 "{",465 " Test.beforeClass();",466 " try {",467 " this.instance = new Test();",468 " this.instance.before();",469 " try {",470 " payload.accept(this.instance);",471 " } finally {",472 " this.instance.after();",473 " }",474 " } finally {",475 " Test.afterClass();",476 " }",477 "}");478 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")479 .withBefore("before")480 .withAfter("after")481 .withBeforeClass("beforeClass")482 .withAfterClass("afterClass")483 .build();484 ClassOrInterfaceDeclaration benchmark =485 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);486 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");487 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));488 }489 @Test490 public void handlesFixtureMethodsWithClassRules() {491 BlockStmt expectedEvaluateBody = blockFromLines(492 "{",493 " Test.beforeClass();",494 " try {",495 " this.benchmark.instance = new Test();",496 " this.benchmark.instance.before();",497 " try {",498 " this.payload.accept(this.benchmark.instance);",499 " } finally {",500 " this.benchmark.instance.after();",501 " }",502 " } finally {",503 " Test.afterClass();",504 " }",505 "}");506 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")507 .withBefore("before")508 .withAfter("after")509 .withBeforeClass("beforeClass")510 .withAfterClass("afterClass")511 .withClassRuleField("rule")512 .build();513 ClassOrInterfaceDeclaration benchmark =514 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);515 BlockStmt evaluateBody = getMethodBody(516 getNestedClass(benchmark, "_ClassStatement"), "evaluate");517 assertThat(evaluateBody, equalsAst(expectedEvaluateBody));518 }519 @Test520 public void handlesFixtureMethodsWithInstanceRules() {521 BlockStmt expectedInstanceEvaluateBody = blockFromLines(522 "{",523 " this.benchmark.instance.before();",524 " try {",525 " this.payload.accept(this.benchmark.instance);",526 " } finally {",527 " this.benchmark.instance.after();",528 " }",529 "}");530 BlockStmt expectedClassEvaluateBody = blockFromLines(531 "{",532 " Test.beforeClass();",533 " try {",534 " this.benchmark.instance = new Test();",535 " org.junit.runners.model.Statement statement =",536 " new _InstanceStatement(this.payload, this.benchmark);",537 " statement = this.applyRule(this.benchmark.instance.rule, statement);",538 " statement.evaluate();",539 " } finally {",540 " Test.afterClass();",541 " }",542 "}");543 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")544 .withBefore("before")545 .withAfter("after")546 .withBeforeClass("beforeClass")547 .withAfterClass("afterClass")548 .withInstanceRuleField("rule")549 .build();550 ClassOrInterfaceDeclaration benchmark =551 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);552 BlockStmt instanceEvaluateBody = getMethodBody(553 getNestedClass(benchmark, "_InstanceStatement"), "evaluate");554 BlockStmt classEvaluateBody = getMethodBody(555 getNestedClass(benchmark, "_ClassStatement"), "evaluate");556 assertThat(instanceEvaluateBody, equalsAst(expectedInstanceEvaluateBody));557 assertThat(classEvaluateBody, equalsAst(expectedClassEvaluateBody));558 }559 @Test560 public void handlesSuperclassFixtureMethodsWithoutRules() {561 BlockStmt expectedRunBenchmarkBody = blockFromLines(562 "{",563 " com.example.TestSuperclass.beforeClass();",564 " try {",565 " this.instance = new Test();",566 " this.instance.before();",567 " try {",568 " payload.accept(this.instance);",569 " } finally {",570 " this.instance.after();",571 " }",572 " } finally {",573 " com.example.TestSuperclass.afterClass();",574 " }",575 "}");576 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")577 .withBefore("before")578 .withAfter("after")579 .withBeforeClass("beforeClass")580 .withAfterClass("afterClass")581 .build();582 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")583 .withSuperclass(testSuperclass)584 .build();585 ClassOrInterfaceDeclaration benchmark =586 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);587 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");588 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));589 }590 @Test591 public void handlesSuperclassFixtureMethodsWithClassRules() {592 BlockStmt expectedEvaluateBody = blockFromLines(593 "{",594 " com.example.TestSuperclass.beforeClass();",595 " try {",596 " this.benchmark.instance = new Test();",597 " this.benchmark.instance.before();",598 " try {",599 " this.payload.accept(this.benchmark.instance);",600 " } finally {",601 " this.benchmark.instance.after();",602 " }",603 " } finally {",604 " com.example.TestSuperclass.afterClass();",605 " }",606 "}");607 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")608 .withBefore("before")609 .withAfter("after")610 .withBeforeClass("beforeClass")611 .withAfterClass("afterClass")612 .build();613 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")614 .withSuperclass(testSuperclass)615 .withClassRuleField("rule")616 .build();617 ClassOrInterfaceDeclaration benchmark =618 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);619 BlockStmt evaluateBody = getMethodBody(620 getNestedClass(benchmark, "_ClassStatement"), "evaluate");621 assertThat(evaluateBody, equalsAst(expectedEvaluateBody));622 }623 @Test624 public void handlesSuperclassFixtureMethodsWithInstanceRules() {625 BlockStmt expectedInstanceEvaluateBody = blockFromLines(626 "{",627 " this.benchmark.instance.before();",628 " try {",629 " this.payload.accept(this.benchmark.instance);",630 " } finally {",631 " this.benchmark.instance.after();",632 " }",633 "}");634 BlockStmt expectedClassEvaluateBody = blockFromLines(635 "{",636 " com.example.TestSuperclass.beforeClass();",637 " try {",638 " this.benchmark.instance = new Test();",639 " org.junit.runners.model.Statement statement =",640 " new _InstanceStatement(this.payload, this.benchmark);",641 " statement = this.applyRule(this.benchmark.instance.rule, statement);",642 " statement.evaluate();",643 " } finally {",644 " com.example.TestSuperclass.afterClass();",645 " }",646 "}");647 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")648 .withBefore("before")649 .withAfter("after")650 .withBeforeClass("beforeClass")651 .withAfterClass("afterClass")652 .build();653 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")654 .withSuperclass(testSuperclass)655 .withInstanceRuleField("rule")656 .build();657 ClassOrInterfaceDeclaration benchmark =658 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);659 BlockStmt instanceEvaluateBody = getMethodBody(660 getNestedClass(benchmark, "_InstanceStatement"), "evaluate");661 BlockStmt classEvaluateBody = getMethodBody(662 getNestedClass(benchmark, "_ClassStatement"), "evaluate");663 assertThat(instanceEvaluateBody, equalsAst(expectedInstanceEvaluateBody));664 assertThat(classEvaluateBody, equalsAst(expectedClassEvaluateBody));665 }666 @Test667 public void handlesMultipleInstanceRules() {668 BlockStmt expectedClassEvaluateBody = blockFromLines(669 "{",670 " this.benchmark.instance = new Test();",671 " org.junit.runners.model.Statement statement =",672 " new _InstanceStatement(this.payload, this.benchmark);",673 " statement = this.applyRule(this.benchmark.instance.rule1, statement);",674 " statement = this.applyRule(this.benchmark.instance.rule2, statement);",675 " statement.evaluate();",676 "}");677 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")678 .withInstanceRuleField("rule1")679 .withInstanceRuleField("rule2")680 .build();681 ClassOrInterfaceDeclaration benchmark =682 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);683 BlockStmt classEvaluateBody = getMethodBody(684 getNestedClass(benchmark, "_ClassStatement"), "evaluate");685 assertThat(classEvaluateBody, equalsAst(expectedClassEvaluateBody));686 }687 @Test688 public void handlesMultipleClassRules() {689 BlockStmt expectedForPayloadBody = blockFromLines(690 "{",691 " org.junit.runner.Description description =",692 " se.chalmers.ju2jmh.api.Rules.description(Test.class, name);",693 " org.junit.runners.model.Statement statement =",694 " new _ClassStatement(payload, benchmark, description);",695 " statement = se.chalmers.ju2jmh.api.Rules.apply(",696 " Test.rule1, statement, description);",697 " statement = se.chalmers.ju2jmh.api.Rules.apply(",698 " Test.rule2, statement, description);",699 " return statement;",700 "}");701 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")702 .withClassRuleField("rule1")703 .withClassRuleField("rule2")704 .build();705 ClassOrInterfaceDeclaration benchmark =706 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);707 BlockStmt forPayloadBody = getMethodBody(708 getNestedClass(benchmark, "_ClassStatement"), "forPayload");709 assertThat(forPayloadBody, equalsAst(expectedForPayloadBody));710 }711 @Test712 public void handlesRuleMethods() {713 BlockStmt expectedClassEvaluateBody = blockFromLines(714 "{",715 " this.benchmark.instance = new Test();",716 " org.junit.runners.model.Statement statement =",717 " new _InstanceStatement(this.payload, this.benchmark);",718 " statement = this.applyRule(this.benchmark.instance.rule(), statement);",719 " statement.evaluate();",720 "}");721 BlockStmt expectedForPayloadBody = blockFromLines(722 "{",723 " org.junit.runner.Description description =",724 " se.chalmers.ju2jmh.api.Rules.description(Test.class, name);",725 " org.junit.runners.model.FrameworkMethod frameworkMethod =",726 " se.chalmers.ju2jmh.api.Rules.frameworkMethod(Test.class, name);",727 " org.junit.runners.model.Statement statement =",728 " new _ClassStatement(payload, benchmark, description, frameworkMethod);",729 " statement = se.chalmers.ju2jmh.api.Rules.apply(",730 " Test.classRule(), statement, description);",731 " return statement;",732 "}");733 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")734 .withInstanceRuleMethod("rule")735 .withClassRuleMethod("classRule")736 .build();737 ClassOrInterfaceDeclaration benchmark =738 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);739 BlockStmt classEvaluateBody = getMethodBody(740 getNestedClass(benchmark, "_ClassStatement"), "evaluate");741 BlockStmt forPayloadBody = getMethodBody(742 getNestedClass(benchmark, "_ClassStatement"), "forPayload");743 assertThat(classEvaluateBody, equalsAst(expectedClassEvaluateBody));744 assertThat(forPayloadBody, equalsAst(expectedForPayloadBody));745 }746 @Test747 public void handlesSuperclassRules() {748 BlockStmt expectedClassEvaluateBody = blockFromLines(749 "{",750 " this.benchmark.instance = new Test();",751 " org.junit.runners.model.Statement statement =",752 " new _InstanceStatement(this.payload, this.benchmark);",753 " statement = this.applyRule(this.benchmark.instance.ruleField, statement);",754 " statement = this.applyRule(this.benchmark.instance.ruleMethod(), statement);",755 " statement.evaluate();",756 "}");757 BlockStmt expectedForPayloadBody = blockFromLines(758 "{",759 " org.junit.runner.Description description =",760 " se.chalmers.ju2jmh.api.Rules.description(Test.class, name);",761 " org.junit.runners.model.FrameworkMethod frameworkMethod =",762 " se.chalmers.ju2jmh.api.Rules.frameworkMethod(Test.class, name);",763 " org.junit.runners.model.Statement statement =",764 " new _ClassStatement(payload, benchmark, description, frameworkMethod);",765 " statement = se.chalmers.ju2jmh.api.Rules.apply(",766 " com.example.TestSuperclass.classRuleField, statement, description);",767 " statement = se.chalmers.ju2jmh.api.Rules.apply(",768 " com.example.TestSuperclass.classRuleMethod(), statement, description);",769 " return statement;",770 "}");771 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")772 .withInstanceRuleField("ruleField")773 .withInstanceRuleMethod("ruleMethod")774 .withClassRuleField("classRuleField")775 .withClassRuleMethod("classRuleMethod")776 .build();777 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")778 .withSuperclass(testSuperclass)779 .build();780 ClassOrInterfaceDeclaration benchmark =781 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);782 BlockStmt classEvaluateBody = getMethodBody(783 getNestedClass(benchmark, "_ClassStatement"), "evaluate");784 BlockStmt forPayloadBody = getMethodBody(785 getNestedClass(benchmark, "_ClassStatement"), "forPayload");786 assertThat(classEvaluateBody, equalsAst(expectedClassEvaluateBody));787 assertThat(forPayloadBody, equalsAst(expectedForPayloadBody));788 }789 @Test790 public void handlesHiddenRuleFields() {791 BlockStmt expectedClassEvaluateBody = blockFromLines(792 "{",793 " this.benchmark.instance = new Test();",794 " org.junit.runners.model.Statement statement =",795 " new _InstanceStatement(this.payload, this.benchmark);",796 " statement = this.applyRule(this.benchmark.instance.rule, statement);",797 " statement = this.applyRule(",798 " ((com.example.TestSuperclass) this.benchmark.instance).rule, statement);",799 " statement.evaluate();",800 "}");801 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")802 .withInstanceRuleField("rule")803 .build();804 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")805 .withSuperclass(testSuperclass)806 .withInstanceRuleField("rule")807 .build();808 ClassOrInterfaceDeclaration benchmark =809 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);810 BlockStmt classEvaluateBody = getMethodBody(811 getNestedClass(benchmark, "_ClassStatement"), "evaluate");812 assertThat(classEvaluateBody, equalsAst(expectedClassEvaluateBody));813 }814 @Test815 public void handlesOverriddenTests() {816 BlockStmt expectedMakePayloadsBody = blockFromLines(817 "{",818 " this.payloads = new _Payloads();",819 " this.payloads.test1 = Test::test1;",820 " this.payloads.test3 = Test::test3;",821 " this.payloads.test4 = Test::test4;",822 " this.payloads.test2 = Test::test2;",823 " this.payloads.test5 = Test::test5;",824 "}");825 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")826 .withTest("test1")827 .withTest("test2")828 .withTest("test3")829 .build();830 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")831 .withSuperclass(testSuperclass)832 .withTest("test4")833 .withTest("test2")834 .withTest("test5")835 .build();836 ClassOrInterfaceDeclaration benchmark =837 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);838 List<String> methodNames = benchmark.getMethods()839 .stream()840 .map(NodeWithSimpleName::getNameAsString)841 .collect(Collectors.toUnmodifiableList());842 BlockStmt makePayloadsBody = getMethodBody(benchmark, "makePayloads");843 assertIterableEquals(844 List.of("benchmark_test1", "benchmark_test3", "benchmark_test4", "benchmark_test2",845 "benchmark_test5", "runBenchmark", "makePayloads"),846 methodNames);847 assertThat(makePayloadsBody, equalsAst(expectedMakePayloadsBody));848 }849 @Test850 public void handlesOverriddenBefore() {851 BlockStmt expectedRunBenchmarkBody = blockFromLines(852 "{",853 " this.instance = new Test();",854 " this.instance.before1();",855 " this.instance.before3();",856 " this.instance.before4();",857 " this.instance.before2();",858 " this.instance.before5();",859 " payload.accept(this.instance);",860 "}");861 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")862 .withBefore("before1")863 .withBefore("before2")864 .withBefore("before3")865 .build();866 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")867 .withSuperclass(testSuperclass)868 .withBefore("before4")869 .withBefore("before2")870 .withBefore("before5")871 .build();872 ClassOrInterfaceDeclaration benchmark =873 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);874 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");875 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));876 }877 @Test878 public void handlesOverriddenAfter() {879 BlockStmt expectedRunBenchmarkBody = blockFromLines(880 "{",881 " this.instance = new Test();",882 " try {",883 " payload.accept(this.instance);",884 " } finally {",885 " this.instance.after4();",886 " this.instance.after2();",887 " this.instance.after5();",888 " this.instance.after1();",889 " this.instance.after3();",890 " }",891 "}");892 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")893 .withAfter("after1")894 .withAfter("after2")895 .withAfter("after3")896 .build();897 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")898 .withSuperclass(testSuperclass)899 .withAfter("after4")900 .withAfter("after2")901 .withAfter("after5")902 .build();903 ClassOrInterfaceDeclaration benchmark =904 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);905 BlockStmt runBenchmarkBody = getMethodBody(benchmark, "runBenchmark");906 assertThat(runBenchmarkBody, equalsAst(expectedRunBenchmarkBody));907 }908 @Test909 public void handlesOverriddenRuleMethods() {910 BlockStmt expectedClassEvaluateBody = blockFromLines(911 "{",912 " this.benchmark.instance = new Test();",913 " org.junit.runners.model.Statement statement =",914 " new _InstanceStatement(this.payload, this.benchmark);",915 " statement = this.applyRule(this.benchmark.instance.rule4(), statement);",916 " statement = this.applyRule(this.benchmark.instance.rule2(), statement);",917 " statement = this.applyRule(this.benchmark.instance.rule5(), statement);",918 " statement = this.applyRule(this.benchmark.instance.rule1(), statement);",919 " statement = this.applyRule(this.benchmark.instance.rule3(), statement);",920 " statement.evaluate();",921 "}");922 UnitTestClass testSuperclass = UnitTestClass.Builder.forClass("com.example.TestSuperclass")923 .withInstanceRuleMethod("rule1")924 .withInstanceRuleMethod("rule2")925 .withInstanceRuleMethod("rule3")926 .build();927 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")928 .withSuperclass(testSuperclass)929 .withInstanceRuleMethod("rule4")930 .withInstanceRuleMethod("rule2")931 .withInstanceRuleMethod("rule5")932 .build();933 ClassOrInterfaceDeclaration benchmark =934 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);935 BlockStmt classEvaluateBody = getMethodBody(936 getNestedClass(benchmark, "_ClassStatement"), "evaluate");937 assertThat(classEvaluateBody, equalsAst(expectedClassEvaluateBody));938 }939 @Test940 public void handlesNameConflicts() {941 ClassOrInterfaceDeclaration expected = classFromLines(942 "@org.openjdk.jmh.annotations.State(org.openjdk.jmh.annotations.Scope.Thread)",943 "public static class _Benchmark_0 {",944 " private _Payloads_1 payloads;",945 " private Test instance;",946 "",947 " @org.openjdk.jmh.annotations.Benchmark",948 " public void benchmark_test() throws java.lang.Throwable {",949 " this.payloads.test.evaluate();",950 " }",951 "",952 " private static class _InstanceStatement_2",953 " extends org.junit.runners.model.Statement {",954 " private final se.chalmers.ju2jmh.api.ThrowingConsumer<Test>",955 " payload;",956 " private final _Benchmark_0 benchmark;",957 "",958 " public _InstanceStatement_2(",959 " se.chalmers.ju2jmh.api.ThrowingConsumer<Test> payload,",960 " _Benchmark_0 benchmark) {",961 " this.payload = payload;",962 " this.benchmark = benchmark;",963 " }",964 "",965 " @java.lang.Override",966 " public void evaluate() throws java.lang.Throwable {",967 " this.payload.accept(this.benchmark.instance);",968 " }",969 " }",970 "",971 " private static class _ClassStatement_3",972 " extends org.junit.runners.model.Statement {",973 " private final se.chalmers.ju2jmh.api.ThrowingConsumer<Test>",974 " payload;",975 " private final _Benchmark_0 benchmark;",976 " private final org.junit.runner.Description description;",977 " private final org.junit.runners.model.FrameworkMethod frameworkMethod;",978 "",979 " private _ClassStatement_3(",980 " se.chalmers.ju2jmh.api.ThrowingConsumer<Test> payload,",981 " _Benchmark_0 benchmark,",982 " org.junit.runner.Description description,",983 " org.junit.runners.model.FrameworkMethod frameworkMethod) {",984 " this.payload = payload;",985 " this.benchmark = benchmark;",986 " this.description = description;",987 " this.frameworkMethod = frameworkMethod;",988 " }",989 "",990 " @java.lang.Override",991 " public void evaluate() throws java.lang.Throwable {",992 " this.benchmark.instance = new Test();",993 " org.junit.runners.model.Statement statement =",994 " new _InstanceStatement_2(this.payload, this.benchmark);",995 " statement = this.applyRule(this.benchmark.instance.rule, statement);",996 " statement.evaluate();",997 " }",998 "",999 " private org.junit.runners.model.Statement applyRule(",1000 " org.junit.rules.TestRule rule,",1001 " org.junit.runners.model.Statement statement) {",1002 " return se.chalmers.ju2jmh.api.Rules.apply(",1003 " rule, statement, this.description);",1004 " }",1005 "",1006 " private org.junit.runners.model.Statement applyRule(",1007 " org.junit.rules.MethodRule rule,",1008 " org.junit.runners.model.Statement statement) {",1009 " return se.chalmers.ju2jmh.api.Rules.apply(",1010 " rule, statement, this.frameworkMethod, this.benchmark.instance);",...

Full Screen

Full Screen

Source:JUnitBridgeObserver.java Github

copy

Full Screen

...45 throws Throwable {46 Test test = eventContext.getEvent();47 Statement statement = new InvokeMethod(null, test.getTestInstance()) {48 @Override49 public void evaluate() {50 eventContext.proceed();51 }52 };53 TestClass arquillianTestClass = test.getTestClass();54 Class<?> clazz = arquillianTestClass.getJavaClass();55 org.junit.runners.model.TestClass junitTestClass =56 new org.junit.runners.model.TestClass(clazz);57 Object target = test.getTestInstance();58 statement = withBefores(59 statement, Before.class, junitTestClass, target);60 statement = withAfters(statement, After.class, junitTestClass, target);61 Method method = test.getTestMethod();62 statement = withRules(63 statement, Rule.class, junitTestClass, target,64 Description.createTestDescription(65 clazz, method.getName(), method.getAnnotations()));66 List<FrameworkMethod> frameworkMethods = new ArrayList<>(67 junitTestClass.getAnnotatedMethods(org.junit.Test.class));68 frameworkMethods.removeAll(69 junitTestClass.getAnnotatedMethods(Ignore.class));70 Collections.sort(frameworkMethods, FrameworkMethodComparator.INSTANCE);71 FrameworkMethod firstFrameworkMethod = frameworkMethods.get(0);72 boolean firstMethod = false;73 if (method.equals(firstFrameworkMethod.getMethod())) {74 firstMethod = true;75 statement = withBefores(76 statement, BeforeClass.class, junitTestClass, null);77 }78 FrameworkMethod lastFrameworkMethod = frameworkMethods.get(79 frameworkMethods.size() - 1);80 boolean lastMethod = false;81 if (method.equals(lastFrameworkMethod.getMethod())) {82 lastMethod = true;83 statement = withAfters(84 statement, AfterClass.class, junitTestClass, null);85 }86 evaluateWithClassRule(87 statement, junitTestClass, target,88 Description.createSuiteDescription(clazz), firstMethod, lastMethod);89 }90 protected void evaluateWithClassRule(91 Statement statement,92 org.junit.runners.model.TestClass junitTestClass, Object target,93 Description description, boolean firstMethod, boolean lastMethod)94 throws Throwable {95 if (!firstMethod && !lastMethod) {96 statement.evaluate();97 return;98 }99 List<TestRule> testRules = junitTestClass.getAnnotatedMethodValues(100 target, ClassRule.class, TestRule.class);101 testRules.addAll(102 junitTestClass.getAnnotatedFieldValues(103 target, ClassRule.class, TestRule.class));104 if (testRules.isEmpty()) {105 statement.evaluate();106 return;107 }108 handleClassRules(testRules, firstMethod, lastMethod, true);109 statement = new RunRules(statement, testRules, description);110 try {111 statement.evaluate();112 }113 finally {114 handleClassRules(testRules, firstMethod, lastMethod, false);115 }116 }117 protected void handleClassRules(118 List<TestRule> testRules, boolean firstMethod, boolean lastMethod,119 boolean enable) {120 for (TestRule testRule : testRules) {121 Class<?> testRuleClass = testRule.getClass();122 if (firstMethod) {123 try {124 Method handleBeforeClassMethod = testRuleClass.getMethod(125 "handleBeforeClass", boolean.class);...

Full Screen

Full Screen

Source:RunAfters.java Github

copy

Full Screen

...15 jadx.core.utils.exceptions.JadxOverflowException: 16 at jadx.core.utils.ErrorsCounter.addError(ErrorsCounter.java:47)17 at jadx.core.utils.ErrorsCounter.methodError(ErrorsCounter.java:81)18 */19 public void evaluate() throws java.lang.Throwable {20 /*21 r7 = this;22 java.util.ArrayList r0 = new java.util.ArrayList23 r0.<init>()24 r1 = 025 org.junit.runners.model.Statement r2 = r7.next // Catch:{ all -> 0x002b }26 r2.evaluate() // Catch:{ all -> 0x002b }27 java.util.List<org.junit.runners.model.FrameworkMethod> r2 = r7.afters28 java.util.Iterator r2 = r2.iterator()29 L_0x0011:30 boolean r3 = r2.hasNext()31 if (r3 == 0) goto L_0x002a32 java.lang.Object r3 = r2.next()33 org.junit.runners.model.FrameworkMethod r3 = (org.junit.runners.model.FrameworkMethod) r334 java.lang.Object r4 = r7.target // Catch:{ all -> 0x0025 }35 java.lang.Object[] r5 = new java.lang.Object[r1] // Catch:{ all -> 0x0025 }36 r3.invokeExplosively(r4, r5) // Catch:{ all -> 0x0025 }37 goto L_0x002938 L_0x0025:39 r4 = move-exception40 r0.add(r4)41 L_0x0029:42 goto L_0x001143 L_0x002a:44 goto L_0x004f45 L_0x002b:46 r2 = move-exception47 r0.add(r2) // Catch:{ all -> 0x0053 }48 java.util.List<org.junit.runners.model.FrameworkMethod> r2 = r7.afters49 java.util.Iterator r2 = r2.iterator()50 L_0x0036:51 boolean r3 = r2.hasNext()52 if (r3 == 0) goto L_0x002a53 java.lang.Object r3 = r2.next()54 org.junit.runners.model.FrameworkMethod r3 = (org.junit.runners.model.FrameworkMethod) r355 java.lang.Object r4 = r7.target // Catch:{ all -> 0x004a }56 java.lang.Object[] r5 = new java.lang.Object[r1] // Catch:{ all -> 0x004a }57 r3.invokeExplosively(r4, r5) // Catch:{ all -> 0x004a }58 goto L_0x004e59 L_0x004a:60 r4 = move-exception61 r0.add(r4)62 L_0x004e:63 goto L_0x003664 L_0x004f:65 org.junit.runners.model.MultipleFailureException.assertEmpty(r0)66 return67 L_0x0053:68 r2 = move-exception69 java.util.List<org.junit.runners.model.FrameworkMethod> r3 = r7.afters70 java.util.Iterator r3 = r3.iterator()71 L_0x005a:72 boolean r4 = r3.hasNext()73 if (r4 == 0) goto L_0x007374 java.lang.Object r4 = r3.next()75 org.junit.runners.model.FrameworkMethod r4 = (org.junit.runners.model.FrameworkMethod) r476 java.lang.Object r5 = r7.target // Catch:{ all -> 0x006e }77 java.lang.Object[] r6 = new java.lang.Object[r1] // Catch:{ all -> 0x006e }78 r4.invokeExplosively(r5, r6) // Catch:{ all -> 0x006e }79 goto L_0x007280 L_0x006e:81 r5 = move-exception82 r0.add(r5)83 L_0x0072:84 goto L_0x005a85 L_0x0073:86 throw r287 */88 throw new UnsupportedOperationException("Method not decompiled: org.junit.internal.runners.statements.RunAfters.evaluate():void");89 }90}...

Full Screen

Full Screen

Source:GuiceyJUnit4.java Github

copy

Full Screen

...30 protected Statement withBeforeClasses(Statement statement) {31 final Statement parent = super.withBeforeClasses(statement);32 return new Statement() {33 @Override34 public void evaluate() throws Throwable {35 manager.beforeClasses();36 parent.evaluate();37 }38 };39 }40 @Override41 protected Statement withAfterClasses(Statement statement) {42 final Statement parent = super.withAfterClasses(statement);43 return new Statement() {44 @Override45 public void evaluate() throws Throwable {46 parent.evaluate();47 manager.afterClasses();48 }49 };50 }51 @Override52 protected Statement withBefores(FrameworkMethod frameworkMethod, final Object test, Statement statement) {53 final Statement parent = super.withBefores(frameworkMethod, test, statement);54 return new Statement() {55 @Override56 public void evaluate() throws Throwable {57 manager.beforeTest(test);58 parent.evaluate();59 }60 };61 }62 @Override63 protected Statement withAfters(FrameworkMethod frameworkMethod, final Object test, Statement statement) {64 final Statement parent = super.withBefores(frameworkMethod, test, statement);65 return new Statement() {66 @Override67 public void evaluate() throws Throwable {68 parent.evaluate();69 manager.afterTest(test);70 }71 };72 }73 @Override74 public void run(RunNotifier runNotifier) {75 super.run(runNotifier);76 }77}...

Full Screen

Full Screen

Source:BladeTestRunner.java Github

copy

Full Screen

...24 protected Statement withBeforeClasses(final Statement statement) {25 final Statement junitStatement = super.withBeforeClasses(statement);26 return new Statement() {27 @Override28 public void evaluate() throws Throwable {29 blade = Blade.me().start(mainCls).await();30 junitStatement.evaluate();31 }32 };33 }34 @Override35 protected Statement withBefores(final FrameworkMethod method, Object target, final Statement statement) {36 Field[] declaredFields = clazz.getDeclaredFields();37 for (Field declaredField : declaredFields) {38 Inject inject = declaredField.getAnnotation(Inject.class);39 if (null != inject) {40 Object bean = blade.getBean(declaredField.getType());41 try {42 declaredField.setAccessible(true);43 declaredField.set(target, bean);44 } catch (IllegalAccessException e) {45 e.printStackTrace();46 }47 }48 }49 final Statement junitStatement = super.withBefores(method, target, statement);50 return new Statement() {51 @Override52 public void evaluate() throws Throwable {53 junitStatement.evaluate();54 }55 };56 }57 @Override58 protected Statement withAfters(final FrameworkMethod method, Object target, final Statement statement) {59 final Statement junitStatement = super.withAfters(method, target, statement);60 return new Statement() {61 @Override62 public void evaluate() throws Throwable {63 junitStatement.evaluate();64 }65 };66 }67 @Override68 protected Statement withAfterClasses(final Statement statement) {69 final Statement junitStatement = super.withAfterClasses(statement);70 return new Statement() {71 @Override72 public void evaluate() throws Throwable {73 junitStatement.evaluate();74 blade.stop();75 }76 };77 }78}...

Full Screen

Full Screen

Source:ExpectsExceptionRunner.java Github

copy

Full Screen

...25 this.expected = expected;26 this.expectedMessage = expectedMessage;27 }28 @Override29 public void evaluate() throws Exception {30 boolean complete = false;31 try {32 next.evaluate();33 complete = true;34 } catch (AssumptionViolatedException e) {35 throw e;36 } catch (Throwable e) {37 if (!expected.isAssignableFrom(e.getClass())) {38 String message = "Unexpected exception, expected<"39 + expected.getName() + "> but was <"40 + e.getClass().getName() + ">";41 throw new Exception(message, e);42 }43 if (isNotNull(expectedMessage) && !expectedMessage.equals(e.getMessage())) {44 String message = "Unexpected exception message, expected<"45 + expectedMessage + "> but was<"46 + e.getMessage() + ">";...

Full Screen

Full Screen

Source:MyRunner.java Github

copy

Full Screen

...21 protected Statement withMyBeforeClasses(final Statement statement) {22 final Statement junitStatement = super.withBeforeClasses(statement);23 return new Statement() {24 @Override25 public void evaluate() throws Throwable {26 System.out.println("Before Class: " + clazz.getName());27 junitStatement.evaluate();28 }29 30 };31 }32 33 // 拦截每一个方法的 Before 事件34 protected Statement withMyBefores(final FrameworkMethod method, Object target, final Statement statement) {35 36 final Statement junitStatement = super.withBefores(method, target, statement);37 return new Statement() {38 @Override39 public void evaluate() throws Throwable {40 System.out.println("Before before method: " + method.getName());41 junitStatement.evaluate();42 System.out.println("After before method: " + method.getName());43 }44 };45 }46 47 // 截获每一个测试方法的 after 事件48 protected Statement withMyAfters(final FrameworkMethod method, Object target, final Statement statement) {49 final Statement junitStatement = super.withAfters(method, target, statement);50 return new Statement() {51 @Override52 public void evaluate() throws Throwable {53 System.out.println("After method: " + method.getName());54 junitStatement.evaluate();55 }56 57 };58 }59 60 // 截获测试类的 after 事件61 protected Statement withMyAfterClasses(final Statement statement) {62 final Statement junitStatement = super.withAfterClasses(statement);63 return new Statement() {64 @Override65 public void evaluate() throws Throwable {66 junitStatement.evaluate();67 System.out.println("After Class: " + clazz.getName());68 }69 };70 }...

Full Screen

Full Screen

Source:ILogStatement.java Github

copy

Full Screen

...4import static com.carmatechnologies.commons.testing.utils.Preconditions.checkNotNull;5/**6 * Decorator around {@link org.junit.runners.model.Statement} which:7 * - 1) enable log capture (to implement in child classes).8 * - 2) evaluate the provided statement (using {@link org.junit.runners.model.Statement#evaluate})9 * - 3) disable log capture (to implement in child classes).10 */11public abstract class ILogStatement extends Statement implements ILogCapturer {12 private final Statement statement;13 public ILogStatement(final Statement statement) {14 this.statement = checkNotNull(statement, "Statement must NOT be null.");15 }16 @Override17 public void evaluate() throws Throwable {18 enableLogCapture();19 try {20 statement.evaluate();21 } finally {22 disableLogCapture();23 }24 }25 abstract public void enableLogCapture();26 abstract public void disableLogCapture();27}...

Full Screen

Full Screen

evaluate

Using AI Code Generation

copy

Full Screen

1Statement statement = new Statement() {2 public void evaluate() throws Throwable {3 System.out.println("Hello World");4 }5};6statement.evaluate();7Statement statement = new Statement() {8 public void evaluate() throws Throwable {9 System.out.println("Hello World");10 }11};12statement.evaluate();

Full Screen

Full Screen

evaluate

Using AI Code Generation

copy

Full Screen

1Statement statement = new Statement() {2 public void evaluate() throws Throwable {3 System.out.println("Running test");4 }5};6statement.evaluate();7System.out.println("Test completed");

Full Screen

Full Screen

evaluate

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.junit.runner.JUnitCore;3import org.junit.runner.Result;4import org.junit.runner.RunWith;5import org.junit.runner.notification.Failure;6import org.junit.runners.BlockJUnit4ClassRunner;7import org.junit.runners.model.Statement;8@RunWith(BlockJUnit4ClassRunner.class)9public class TestRunner {10 public static void main(String[] args) {11 Result result = JUnitCore.runClasses(TestRunner.class);12 for (Failure failure : result.getFailures()) {13 System.out.println(failure.toString());14 }15 System.out.println(result.wasSuccessful());16 }17 public void test1() throws InterruptedException {18 System.out.println("test1");19 Thread.sleep(5000);20 System.out.println("test1");21 }22 public void test2() throws InterruptedException {23 System.out.println("test2");24 Thread.sleep(5000);25 System.out.println("test2");26 }27 public Statement methodBlock(org.junit.runners.model.FrameworkMethod method) {28 Statement statement = super.methodBlock(method);29 return new ThreadStatement(statement);30 }31 private static class ThreadStatement extends Statement {32 private final Statement statement;33 public ThreadStatement(Statement statement) {34 this.statement = statement;35 }36 public void evaluate() throws Throwable {37 Thread thread = new Thread(new Runnable() {38 public void run() {39 try {40 statement.evaluate();41 } catch (Throwable e) {42 e.printStackTrace();43 }44 }45 });46 thread.start();47 thread.join();48 }49 }50}

Full Screen

Full Screen

JUnit Tutorial:

LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.

JUnit Tutorial Chapters:

Here are the detailed JUnit testing chapters to help you get started:

  • Importance of Unit testing - Learn why Unit testing is essential during the development phase to identify bugs and errors.
  • Top Java Unit testing frameworks - Here are the upcoming JUnit automation testing frameworks that you can use in 2023 to boost your unit testing.
  • What is the JUnit framework
  • Why is JUnit testing important - Learn the importance and numerous benefits of using the JUnit testing framework.
  • Features of JUnit - Learn about the numerous features of JUnit and why developers prefer it.
  • JUnit 5 vs. JUnit 4: Differences - Here is a complete comparison between JUnit 5 and JUnit 4 testing frameworks.
  • Setting up the JUnit environment - Learn how to set up your JUnit testing environment.
  • Getting started with JUnit testing - After successfully setting up your JUnit environment, this chapter will help you get started with JUnit testing in no time.
  • Parallel testing with JUnit - Parallel Testing can be used to reduce test execution time and improve test efficiency. Learn how to perform parallel testing with JUnit.
  • Annotations in JUnit - When writing automation scripts with JUnit, we can use JUnit annotations to specify the type of methods in our test code. This helps us identify those methods when we run JUnit tests using Selenium WebDriver. Learn in detail what annotations are in JUnit.
  • Assertions in JUnit - Assertions are used to validate or test that the result of an action/functionality is the same as expected. Learn in detail what assertions are and how to use them while performing JUnit testing.
  • Parameterization in JUnit - Parameterized Test enables you to run the same automated test scripts with different variables. By collecting data on each method's test parameters, you can minimize time spent on writing tests. Learn how to use parameterization in JUnit.
  • Nested Tests In JUnit 5 - A nested class is a non-static class contained within another class in a hierarchical structure. It can share the state and setup of the outer class. Learn about nested annotations in JUnit 5 with examples.
  • Best practices for JUnit testing - Learn about the best practices, such as always testing key methods and classes, integrating JUnit tests with your build, and more to get the best possible results.
  • Advanced Use Cases for JUnit testing - Take a deep dive into the advanced use cases, such as how to run JUnit tests in Jupiter, how to use JUnit 5 Mockito for Unit testing, and more for JUnit testing.

JUnit Certification:

You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Statement

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful