Best junit code snippet using org.junit.runner.Description.getChildren
Source:CategoryWithParameterizedRunnerFactoryTest.java
...60 public ExposedParameterized(Class<?> klass) throws Throwable {61 super(klass);62 }63 @Override64 protected List<Runner> getChildren() {65 return super.getChildren();66 }67 }68 @RunWith(ExposedParameterized.class)69 @Parameterized.UseParametersRunnerFactory(ExposedBlockJUnit4ClassRunnerWithParametersFactory.class)70 public static class BrokenCategoryClass {71 @Parameterized.Parameters72 public static Iterable<String> getParams() {73 return Arrays.asList("one", "two");74 }75 @Parameterized.Parameter76 public String value;77 @Test78 public void insanity() {79 assertTrue(true);80 }81 }82 @RunWith(ExposedParameterized.class)83 @Parameterized.UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)84 public static class WorkingCategoryClass {85 @Parameterized.Parameters86 public static Iterable<String> getParams() {87 return Arrays.asList("one", "two");88 }89 @Parameterized.Parameter90 public String value;91 @Test92 public void insanity() {93 assertTrue(true);94 }95 }96 @Test97 public void testBrokenCategoryAndParameterized() {98 Request request = Request.aClass(BrokenCategoryClass.class);99 ExposedParameterized runner = (ExposedParameterized) request.getRunner();100 request = request.filterWith(new CategoryFilter(101 (ExposedBlockJUnit4ClassRunnerWithParameters) runner.getChildren().get(0)));102 Result result = new JUnitCore().run(request);103 assertEquals(2, result.getRunCount());104 }105 @Test106 public void testWorkingCategoryAndParameterized() {107 Request request = Request.aClass(WorkingCategoryClass.class);108 ExposedParameterized runner = (ExposedParameterized) request.getRunner();109 request =110 request.filterWith(new CategoryFilter((ExposedGetAnnotations) runner.getChildren().get(0)));111 Result result = new JUnitCore().run(request);112 assertEquals(2, result.getRunCount());113 }114 public static class CategoryFilter extends Filter {115 private ExposedGetAnnotations runner;116 public CategoryFilter(ExposedGetAnnotations runner) {117 this.runner = runner;118 }119 @Override120 public boolean shouldRun(Description description) {121 if (description.getChildren().size() == 0) {122 return true;123 }124 List<Annotation> runnerAnnotations = new ArrayList<>();125 Collections.addAll(runnerAnnotations, runner.getRunnerAnnotations());126 for (Annotation a : description.getAnnotations()) {127 if (runnerAnnotations.contains(a)) {128 return true;129 }130 }131 return false;132 }133 @Override134 public String describe() {135 return CategoryFilter.class.getSimpleName();...
Source:JUnit4TestReference.java
...38 if (description.isTest()) {39 return 1;40 } else {41 int result= 0;42 for (Description child : description.getChildren()) {43 result+= countTestCases(child);44 }45 return result;46 }47 }48 @Override49 public boolean equals(Object obj) {50 if (!(obj instanceof JUnit4TestReference))51 return false;52 JUnit4TestReference ref= (JUnit4TestReference)obj;53 return (ref.fRoot.equals(fRoot));54 }55 public ITestIdentifier getIdentifier() {56 return new JUnit4Identifier(fRoot);57 }58 @Override59 public int hashCode() {60 return fRoot.hashCode();61 }62 public void run(TestExecution execution) {63 final RunNotifier notifier= new RunNotifier();64 notifier.addListener(new JUnit4TestListener(execution.getListener()));65 execution.addStopListener(new IStopListener() {66 public void stop() {67 notifier.pleaseStop();68 }69 });70 Result result= new Result();71 RunListener listener= result.createListener();72 notifier.addListener(listener);73 try {74 notifier.fireTestRunStarted(fRunner.getDescription());75 fRunner.run(notifier);76 notifier.fireTestRunFinished(result);77 } catch (StoppedByUserException e) {78 // not interesting, see https://bugs.eclipse.org/32949879 } finally {80 notifier.removeListener(listener);81 }82 }83 public void sendTree(IVisitsTestTrees notified) {84 sendTree(notified, fRoot);85 }86 private void sendTree(final IVisitsTestTrees notified, Description description) {87 if (description.isTest()) {88 notified.visitTreeEntry(new JUnit4Identifier(description), false, 1, false, "-1"); //$NON-NLS-1$89 } else {90 notified.visitTreeEntry(new JUnit4Identifier(description), true, description.getChildren().size(), false, "-1"); //$NON-NLS-1$91 for (Description child : description.getChildren()) {92 sendTree(notified, child);93 }94 }95 }96 @Override97 public String toString() {98 return fRoot.toString();99 }100}...
Source:FilterableTest.java
...29 @Test30 public void shouldReturnOneDescriptionForSimpleTestCase() throws Exception {31 Request request = requestSingleMethodRun(SampleTestCase.class, "firstTestMethod");32 Description description = request.getRunner().getDescription();33 assertThat(description.getChildren()).hasSize(1);34 assertThat(description.getChildren().get(0).getChildren()).hasSize(0);35 }36 @Test37 public void shouldReturnParametrizedDescriptionsForParametrizedTestCase() throws Exception {38 Request request = requestSingleMethodRun(SampleTestCase.class, "secondTestMethod");39 Description description = request.getRunner().getDescription();40 assertThat(description.getChildren()).hasSize(1);41 assertThat(description.getChildren().get(0).getChildren()).hasSize(2);42 }43 @Test44 public void shouldApplyFiltersCumulatively() throws Exception {45 JUnitParamsRunner runner = new JUnitParamsRunner(SampleTestCase.class);46 // Remove the first method.47 new SingleMethodFilter("firstTestMethod").apply(runner);48 try {49 // Now remove all instances of the second method.50 new SingleMethodFilter("secondTestMethod").apply(runner);51 fail("Filtering did not apply cumulatively");52 } catch (NoTestsRemainException expected) {53 // expected54 }55 }...
Source:6660.java
...
Source:JUnit4TestAdapterCache.java
...28 if (description.isTest()) {29 return new JUnit4TestCaseFacade(description);30 }31 TestSuite suite = new TestSuite(description.getDisplayName());32 Iterator<Description> it = description.getChildren().iterator();33 while (it.hasNext()) {34 suite.addTest(asTest(it.next()));35 }36 return suite;37 }38 public RunNotifier getNotifier(final TestResult result, JUnit4TestAdapter adapter) {39 RunNotifier notifier = new RunNotifier();40 notifier.addListener(new RunListener() {41 /* class junit.framework.JUnit4TestAdapterCache.AnonymousClass1 */42 @Override // org.junit.runner.notification.RunListener43 public void testFailure(Failure failure) throws Exception {44 result.addError(JUnit4TestAdapterCache.this.asTest(failure.getDescription()), failure.getException());45 }46 @Override // org.junit.runner.notification.RunListener47 public void testFinished(Description description) throws Exception {48 result.endTest(JUnit4TestAdapterCache.this.asTest(description));49 }50 @Override // org.junit.runner.notification.RunListener51 public void testStarted(Description description) throws Exception {52 result.startTest(JUnit4TestAdapterCache.this.asTest(description));53 }54 });55 return notifier;56 }57 public List<Test> asTestList(Description description) {58 if (description.isTest()) {59 return Arrays.asList(asTest(description));60 }61 List<Test> returnThis = new ArrayList<>();62 Iterator<Description> it = description.getChildren().iterator();63 while (it.hasNext()) {64 returnThis.add(asTest(it.next()));65 }66 return returnThis;67 }68}...
Source:SerializableDescription.java
...40 */41 @SuppressWarnings("unchecked")42 public static SerializableDescription create(Description description) {43 final List<SerializableDescription> children;44 if (description.getChildren() != null) {45 children = new ArrayList<SerializableDescription>();46 for (Description child : description.getChildren()) {47 children.add(create(child));48 }49 } else {50 children = Collections.EMPTY_LIST;51 }52 return new SerializableDescription(description.getDisplayName(), children);5354 }5556 /**57 * Restore an org.junit.runner.Description from this SerializableDescription object58 * @return the restored org.junit.runner.Description object59 */60 public Description restore() {
...
Source:ParameterizedNamesTest.java
...33 }34 @Test35 public void parameterizedTestsWithSpecialCharsInName() {36 Request request = Request.aClass(ParametrizedWithSpecialCharsInName.class);37 for (Description parent : request.getRunner().getDescription().getChildren()) {38 for (Description description : parent.getChildren()) {39 assertEquals("test" + parent.getDisplayName(), description.getMethodName());40 }41 }42 }43}
getChildren
Using AI Code Generation
1import org.junit.runner.Description;2import org.junit.runner.RunWith;3import org.junit.runners.Parameterized;4import org.junit.runners.Parameterized.Parameters;5import org.junit.runners.Suite;6import org.junit.runners.Suite.SuiteClasses;7@RunWith(Parameterized.class)8public class JunitParameterizedTest {9 private int number;10 public JunitParameterizedTest(int number) {11 this.number = number;12 }13 public static Iterable<Object[]> data1() {14 return Arrays.asList(new Object[][] { { 1 }, { 2 }, { 3 }, { 4 }, { 5 } });15 }16 public void test() {17 System.out.println("Parameterized Number is : " + number);18 }19}20import org.junit.runner.Description;21import org.junit.runner.notification.RunNotifier;22import org.junit.runners.Parameterized;23import org.junit.runners.Parameterized.Parameters;24import org.junit.runners.Suite;25import org.junit.runners.Suite.SuiteClasses;26import org.junit.runners.model.InitializationError;27public class JunitParameterizedTestRunner extends Parameterized {28 public JunitParameterizedTestRunner(Class<?> klass) throws Throwable {29 super(klass);30 }31 public void run(RunNotifier notifier) {32 Description description = getDescription();33 System.out.println("Description: " + description);34 System.out.println("Children: " + description.getChildren());35 System.out.println("Display Name: " + description.getDisplayName());36 System.out.println("Test Count: " + description.testCount());37 super.run(notifier);38 }39 public static Iterable<Object[]> data1() {40 return Arrays.asList(new Object[][] { { 1 }, { 2 }, { 3 }, { 4 }, { 5 } });41 }42}43Children: [JunitParameterizedTestRunner(1), JunitParameterizedTestRunner(2), JunitParameterizedTestRunner(3), JunitParameterizedTestRunner(4), JunitParameterizedTestRunner(5)]
getChildren
Using AI Code Generation
1import org.junit.runner.Description;2import org.junit.runner.RunWith;3import org.junit.runners.Parameterized;4import org.junit.runners.Parameterized.Parameters;5import org.junit.runners.Parameterized.Parameter;6import org.junit.Test;7import java.util.Arrays;8import java.util.Collection;9import static org.junit.Assert.assertEquals;10@RunWith(Parameterized.class)11public class TestDescription {12 public static Collection<Object[]> data() {13 return Arrays.asList(new Object[][] {14 { "class", "org.junit.runner.DescriptionTest", "org.junit.runner.DescriptionTest" },15 { "method", "org.junit.runner.DescriptionTest", "org.junit.runner.DescriptionTest#testDescription" },16 { "nestedClass", "org.junit.runner.DescriptionTest$Nested", "org.junit.runner.DescriptionTest$Nested" },17 { "nestedMethod", "org.junit.runner.DescriptionTest$Nested", "org.junit.runner.DescriptionTest$Nested#testDescription" },18 });19 }20 public String type;21 @Parameter(1)22 public String className;23 @Parameter(2)24 public String displayName;25 public void testDescription() throws Exception {26 Class<?> clazz = Class.forName(className);27 Description description = Description.createSuiteDescription(clazz);28 if ("method".equals(type)) {29 description.addChild(Description.createTestDescription(clazz, "testDescription"));30 } else if ("nestedClass".equals(type)) {31 description.addChild(Description.createSuiteDescription(clazz.getDeclaredClasses()[0]));32 } else if ("nestedMethod".equals(type)) {33 description.getChildren().get(0).addChild(Description.createTestDescription(clazz.getDeclaredClasses()[0], "testDescription"));34 }35 assertEquals(displayName, description.getDisplayName());36 }37 public static class Nested {38 public void testDescription() {39 }40 }41}
getChildren
Using AI Code Generation
1import org.junit.runner.Description;2import org.junit.runner.RunWith;3import org.junit.runners.Suite;4import org.junit.runners.Suite.SuiteClasses;5import org.junit.runners.model.InitializationError;6@RunWith(Suite.class)7@SuiteClasses({ Junit4Test.class })8public class Junit4TestSuite extends Suite {9 public Junit4TestSuite(Class<?> klass) throws InitializationError {10 super(klass, getChildren());11 }12 private static Class<?>[] getChildren() {13 Description description = Description.createSuiteDescription("Junit4TestSuite");14 Junit4Test junit4Test = new Junit4Test();15 junit4Test.test1();16 junit4Test.test2();17 junit4Test.test3();18 junit4Test.test4();19 junit4Test.test5();20 junit4Test.test6();21 junit4Test.test7();22 junit4Test.test8();23 junit4Test.test9();24 junit4Test.test10();25 junit4Test.test11();26 junit4Test.test12();27 junit4Test.test13();28 junit4Test.test14();29 junit4Test.test15();30 junit4Test.test16();31 junit4Test.test17();32 junit4Test.test18();33 junit4Test.test19();34 junit4Test.test20();35 junit4Test.test21();36 junit4Test.test22();37 junit4Test.test23();38 junit4Test.test24();39 junit4Test.test25();40 junit4Test.test26();41 junit4Test.test27();42 junit4Test.test28();43 junit4Test.test29();44 junit4Test.test30();45 junit4Test.test31();46 junit4Test.test32();47 junit4Test.test33();48 junit4Test.test34();49 junit4Test.test35();50 junit4Test.test36();51 junit4Test.test37();52 junit4Test.test38();53 junit4Test.test39();54 junit4Test.test40();55 junit4Test.test41();56 junit4Test.test42();57 junit4Test.test43();58 junit4Test.test44();59 junit4Test.test45();60 junit4Test.test46();61 junit4Test.test47();62 junit4Test.test48();63 junit4Test.test49();64 junit4Test.test50();65 junit4Test.test51();66 junit4Test.test52();67 junit4Test.test53();
getChildren
Using AI Code Generation
1import org.junit.runner.Description;2import org.junit.runner.Runner;3import org.junit.runner.notification.RunNotifier;4import org.junit.runners.Suite;5import org.junit.runners.model.InitializationError;6import java.util.List;7public class MySuiteRunner extends Suite {8 public MySuiteRunner(Class<?> klass) throws InitializationError {9 super(klass, new Runner[]{new MyRunner(klass)});10 }11 private static class MyRunner extends Runner {12 private final Class<?> klass;13 private final Description description;14 private MyRunner(Class<?> klass) throws InitializationError {15 this.klass = klass;16 this.description = Description.createSuiteDescription(klass);17 for (Description child : this.description.getChildren()) {18 System.out.println(child.getDisplayName());19 }20 }21 public Description getDescription() {22 return this.description;23 }24 public void run(RunNotifier notifier) {25 }26 }27}
getChildren
Using AI Code Generation
1import org.junit.runner.Description;2import org.junit.runner.JUnitCore;3import org.junit.runner.Result;4import org.junit.runner.RunWith;5import org.junit.runners.Suite;6import org.junit.runners.Suite.SuiteClasses;7@RunWith(Suite.class)8@SuiteClasses({ TestClass1.class, TestClass2.class })9public class TestSuite {10 public static void main(String[] args) {11 JUnitCore junit = new JUnitCore();12 Result result = junit.run(TestSuite.class);13 Description suiteDescription = result.getRunDescription();14 for (Description description : suiteDescription.getChildren()) {15 System.out.println(description.getDisplayName());16 }17 }18}19package com.journaldev.junit;20import org.junit.Test;21public class TestClass1 {22 public void testMethod1() {23 System.out.println("TestClass1.testMethod1()");24 }25 public void testMethod2() {26 System.out.println("TestClass1.testMethod2()");27 }28}29package com.journaldev.junit;30import org.junit.Test;31public class TestClass2 {32 public void testMethod3() {33 System.out.println("TestClass2.testMethod3()");34 }35 public void testMethod4() {36 System.out.println("TestClass2.testMethod4()");37 }38}39TestClass1.testMethod1()40TestClass1.testMethod2()41TestClass2.testMethod3()42TestClass2.testMethod4()
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.
Here are the detailed JUnit testing chapters to help you get started:
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.
Get 100 minutes of automation test minutes FREE!!