How to use equals method of org.junit.runners.model.Test class

Best junit code snippet using org.junit.runners.model.Test.equals

Source:TailoredBenchmarkFactoryTest.java Github

copy

Full Screen

...14import static org.hamcrest.MatcherAssert.assertThat;15import static org.junit.jupiter.api.Assertions.assertFalse;16import static org.junit.jupiter.api.Assertions.assertIterableEquals;17import static org.junit.jupiter.api.Assertions.assertTrue;18import static se.chalmers.ju2jmh.AstMatcher.equalsAst;19public class TailoredBenchmarkFactoryTest {20 private static CompilationUnit compilationUnitFromLines(String... lines) {21 return StaticJavaParser.parse(String.join("\n", lines));22 }23 private static ClassOrInterfaceDeclaration classFromLines(String... lines) {24 return StaticJavaParser.parseTypeDeclaration(String.join("\n", lines))25 .asClassOrInterfaceDeclaration();26 }27 private static MethodDeclaration methodFromLines(String... lines) {28 return StaticJavaParser.parseMethodDeclaration(String.join("\n", lines));29 }30 private static BlockStmt blockFromLines(String... lines) {31 return StaticJavaParser.parseBlock(String.join("\n", lines));32 }33 private static MethodDeclaration getMethod(34 ClassOrInterfaceDeclaration declaringClass, String name) {35 return declaringClass.getMethodsByName(name).get(0);36 }37 private static BlockStmt getMethodBody(38 ClassOrInterfaceDeclaration declaringClass, String name) {39 return getMethod(declaringClass, name).getBody().orElseThrow();40 }41 private static ClassOrInterfaceDeclaration getNestedClass(42 ClassOrInterfaceDeclaration parent, String name) {43 return parent.getMembers()44 .stream()45 .filter(BodyDeclaration::isClassOrInterfaceDeclaration)46 .map(BodyDeclaration::asClassOrInterfaceDeclaration)47 .filter(c -> c.getNameAsString().equals(name))48 .findFirst()49 .orElseThrow();50 }51 @Test52 public void handlesSimpleTest() {53 ClassOrInterfaceDeclaration expected = classFromLines(54 "@org.openjdk.jmh.annotations.State(org.openjdk.jmh.annotations.Scope.Thread)",55 "public static class _Benchmark {",56 " private _Payloads payloads;",57 " private SimpleTest instance;",58 "",59 " @org.openjdk.jmh.annotations.Benchmark",60 " public void benchmark_test() throws java.lang.Throwable {",61 " this.runBenchmark(this.payloads.test);",62 " }",63 "",64 " private void runBenchmark(",65 " se.chalmers.ju2jmh.api.ThrowingConsumer<SimpleTest> payload)",66 " throws java.lang.Throwable {",67 " this.instance = new SimpleTest();",68 " payload.accept(this.instance);",69 " }",70 "",71 " private static class _Payloads {",72 " public se.chalmers.ju2jmh.api.ThrowingConsumer<SimpleTest> test;",73 " }",74 "",75 " @org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",76 " public void makePayloads() {",77 " this.payloads = new _Payloads();",78 " this.payloads.test = SimpleTest::test;",79 " }",80 "}"81 );82 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.SimpleTest")83 .withTest("test")84 .build();85 ClassOrInterfaceDeclaration benchmark =86 TailoredBenchmarkFactory.generateBenchmarkClass(testClass);87 assertThat(benchmark, equalsAst(expected));88 }89 @Test90 public void handlesClassRule() {91 ClassOrInterfaceDeclaration expected = classFromLines(92 "@org.openjdk.jmh.annotations.State(org.openjdk.jmh.annotations.Scope.Thread)",93 "public static class _Benchmark {",94 " private _Payloads payloads;",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);",1011 " }",1012 "",1013 " public static org.junit.runners.model.Statement forPayload(",1014 " se.chalmers.ju2jmh.api.ThrowingConsumer<Test> payload,",1015 " String name,",1016 " _Benchmark_0 benchmark) {",1017 " org.junit.runner.Description description =",1018 " se.chalmers.ju2jmh.api.Rules.description(Test.class, name);",1019 " org.junit.runners.model.FrameworkMethod frameworkMethod =",1020 " se.chalmers.ju2jmh.api.Rules.frameworkMethod(Test.class, name);",1021 " org.junit.runners.model.Statement statement =",1022 " new _ClassStatement_3(payload, benchmark, description, frameworkMethod);",1023 " return statement;",1024 " }",1025 " }",1026 "",1027 " private static class _Payloads_1 {",1028 " public org.junit.runners.model.Statement test;",1029 " }",1030 "",1031 " @org.openjdk.jmh.annotations.Setup(org.openjdk.jmh.annotations.Level.Trial)",1032 " public void makePayloads() {",1033 " this.payloads = new _Payloads_1();",1034 " this.payloads.test = _ClassStatement_3.forPayload(Test::test, \"test\", this);",1035 " }",1036 "}"1037 );1038 UnitTestClass testClass = UnitTestClass.Builder.forClass("com.example.Test")1039 .withTest("test")1040 .withInstanceRuleField("rule")1041 .build();1042 Predicate<String> nameValidator = n -> {1043 switch (n) {1044 case "_Benchmark":1045 case "_Payloads":1046 case "_Payloads_0":1047 case "_InstanceStatement":1048 case "_InstanceStatement_0":1049 case "_InstanceStatement_1":1050 case "_ClassStatement":1051 case "_ClassStatement_0":1052 case "_ClassStatement_1":1053 case "_ClassStatement_2":1054 return false;1055 default:1056 return true;1057 }1058 };1059 ClassOrInterfaceDeclaration benchmark =1060 TailoredBenchmarkFactory.generateBenchmarkClass(testClass, nameValidator);1061 assertThat(benchmark, equalsAst(expected));1062 }1063 @Test1064 public void generatedNameValidatorCatchesAllIdentifiers() {1065 CompilationUnit compilationUnit = compilationUnitFromLines(1066 "package _i0._i1;",1067 "",1068 "import _i2._i3;",1069 "",1070 "public class _i4<_i5 extends _i6 & _i7> extends _i8 {",1071 " public enum _i9 {",1072 " _i10",1073 " }",1074 "",1075 " public @interface _i11 {",...

Full Screen

Full Screen

Source:BlockJUnit4ClassRunnerOverrideTest.java Github

copy

Full Screen

...82 */83 public static class OverrideCreateTestRunner extends BlockJUnit4ClassRunner {84 public OverrideCreateTestRunner(final Class<?> klass) throws InitializationError {85 super(klass);86 assert(klass.equals(OverrideCreateTest.class));87 }88 @Override89 protected Object createTest(FrameworkMethod method) {90 final OverrideCreateTest obj = new OverrideCreateTest();91 obj.method = method;92 return obj;93 }94 }95 @RunWith(OverrideCreateTestRunner.class)96 public static class OverrideCreateTest {97 public FrameworkMethod method;98 @Test99 public void testMethodA() {100 assertEquals("testMethodA", method.getMethod().getName());101 }102 @Test103 public void testMethodB() {104 assertEquals("testMethodB", method.getMethod().getName());105 }106 }107 @Test108 public void overrideCreateTestMethod() {109 assertThat(testResult(OverrideCreateTest.class), isSuccessful());110 }111 /**112 * Runner for testing override of {@link org.junit.runners.BlockJUnit4ClassRunner#createTest()}113 * is still called by default if no other {@code createTest} method override114 * is in place. This is tested by setting a boolean flag in a field of the115 * test class so it can be checked to confirm that the createTest method was116 * called.117 */118 public static class CreateTestDefersToNoArgCreateTestRunner extends BlockJUnit4ClassRunner {119 public CreateTestDefersToNoArgCreateTestRunner(final Class<?> klass) throws InitializationError {120 super(klass);121 assert(klass.equals(CreateTestDefersToNoArgCreateTestTest.class));122 }123 @Override124 protected Object createTest() {125 final CreateTestDefersToNoArgCreateTestTest obj = new CreateTestDefersToNoArgCreateTestTest();126 obj.createTestCalled = true;127 return obj;128 }129 }130 @RunWith(CreateTestDefersToNoArgCreateTestRunner.class)131 public static class CreateTestDefersToNoArgCreateTestTest {132 public boolean createTestCalled = false;133 @Test134 public void testCreateTestCalled() {135 assertEquals(true, createTestCalled);...

Full Screen

Full Screen

Source:CustomBlockJUnit4ClassRunnerTest.java Github

copy

Full Screen

...41 super(testClass);42 }43 @Override44 protected Statement methodBlock(FrameworkMethod method) {45 if ("throwException".equals(method.getName())) {46 throw new RuntimeException("throwException() test method invoked");47 }48 return super.methodBlock(method);49 }50 }51 /**52 * Simple {@link RunListener} that tracks the number of times that53 * certain callbacks are invoked.54 */55 private static class TrackingRunListener extends RunListener {56 final AtomicInteger testStartedCount = new AtomicInteger();57 final AtomicInteger testFailureCount = new AtomicInteger();58 final AtomicInteger testFinishedCount = new AtomicInteger();59 @Override...

Full Screen

Full Screen

Source:WebMvcTestPrintDefaultRunner.java Github

copy

Full Screen

...34 protected Statement methodBlock(FrameworkMethod frameworkMethod) {35 Statement statement = super.methodBlock(frameworkMethod);36 statement = new AlwaysPassStatement(statement);37 OutputCapture outputCapture = new OutputCapture();38 if (frameworkMethod.getName().equals("shouldPrint")) {39 outputCapture.expect(containsString("HTTP Method"));40 }41 else if (frameworkMethod.getName().equals("shouldNotPrint")) {42 outputCapture.expect(not(containsString("HTTP Method")));43 }44 else {45 throw new IllegalStateException("Unexpected test method");46 }47 System.err.println(frameworkMethod.getName());48 return outputCapture.apply(statement, null);49 }50 private static class AlwaysPassStatement extends Statement {51 private final Statement delegate;52 AlwaysPassStatement(Statement delegate) {53 this.delegate = delegate;54 }55 @Override...

Full Screen

Full Screen

equals

Using AI Code Generation

copy

Full Screen

1public void testEqualsMethod() {2 Test test1 = new Test() {3 public String getName() {4 return "test1";5 }6 public void run(RunNotifier notifier) {7 }8 };9 Test test2 = new Test() {10 public String getName() {11 return "test2";12 }13 public void run(RunNotifier notifier) {14 }15 };16 Test test3 = new Test() {17 public String getName() {18 return "test1";19 }20 public void run(RunNotifier notifier) {21 }22 };23 List<Test> tests = new ArrayList<Test>();24 tests.add(test1);25 tests.add(test2);26 tests.add(test3);27 for (int i = 0; i < tests.size(); i++) {28 for (int j = i + 1; j < tests.size(); j++) {29 if (tests.get(i).equals(tests.get(j))) {30 tests.remove(j);31 }32 }33 }34 for (Test test : tests) {35 System.out.println(test.getName());36 }37}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful