How to use getDescription method of junit.framework.JUnit4TestAdapter class

Best junit code snippet using junit.framework.JUnit4TestAdapter.getDescription

Run junit automation tests on LambdaTest cloud grid

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

copy
1package org.junit.internal.runners;
2
3import junit.extensions.TestDecorator;
4import junit.framework.AssertionFailedError;
5import junit.framework.JUnit4TestAdapter;
6import junit.framework.JUnit4TestCaseFacade;
7import junit.framework.Test;
8import junit.framework.TestCase;
9import junit.framework.TestListener;
10import junit.framework.TestResult;
11import junit.framework.TestSuite;
12import org.junit.runner.Description;
13import org.junit.runner.Runner;
14import org.junit.runner.notification.Failure;
15import org.junit.runner.notification.RunNotifier;
16
17public class OldTestClassRunner extends Runner {
18	private static final class OldTestClassAdaptingListener implements
19			TestListener {
20		private final RunNotifier fNotifier;
21
22		private OldTestClassAdaptingListener(RunNotifier notifier) {
23			fNotifier= notifier;
24		}
25
26		public void endTest(Test test) {
27			fNotifier.fireTestFinished(asDescription(test));
28		}
29
30		public void startTest(Test test) {
31			fNotifier.fireTestStarted(asDescription(test));
32		}
33
34		// Implement junit.framework.TestListener
35		public void addError(Test test, Throwable t) {
36			Failure failure= new Failure(asDescription(test), t);
37			fNotifier.fireTestFailure(failure);
38		}
39
40		private Description asDescription(Test test) {
41			if (test instanceof JUnit4TestCaseFacade) {
42				JUnit4TestCaseFacade facade= (JUnit4TestCaseFacade) test;
43				return facade.getDescription();
44			}
45			return Description.createTestDescription(test.getClass(), getName(test));
46		}
47
48		private String getName(Test test) {
49			if (test instanceof TestCase)
50				return ((TestCase) test).getName();
51			else
52				return test.toString();
53		}
54
55		public void addFailure(Test test, AssertionFailedError t) {
56			addError(test, t);
57		}
58	}
59
60	private Test fTest;
61	
62	@SuppressWarnings("unchecked")
63	public OldTestClassRunner(Class<?> klass) {
64		this(new TestSuite(klass.asSubclass(TestCase.class)));
65	}
66
67	public OldTestClassRunner(Test test) {
68		super();
69		fTest= test;
70	}
71
72	@Override
73	public void run(RunNotifier notifier) {
74		TestResult result= new TestResult();
75		result.addListener(createAdaptingListener(notifier));
76		fTest.run(result);
77	}
78
79	public static TestListener createAdaptingListener(final RunNotifier notifier) {
80		return new OldTestClassAdaptingListener(notifier);
81	}
82	
83	@Override
84	public Description getDescription() {
85		return makeDescription(fTest);
86	}
87
88	private Description makeDescription(Test test) {
89		if (test instanceof TestCase) {
90			TestCase tc= (TestCase) test;
91			return Description.createTestDescription(tc.getClass(), tc.getName());
92		} else if (test instanceof TestSuite) {
93			TestSuite ts= (TestSuite) test;
94			String name= ts.getName() == null ? "" : ts.getName();
95			Description description= Description.createSuiteDescription(name);
96			int n= ts.testCount();
97			for (int i= 0; i < n; i++)
98				description.addChild(makeDescription(ts.testAt(i)));
99			return description;
100		} else if (test instanceof JUnit4TestAdapter) {
101			JUnit4TestAdapter adapter= (JUnit4TestAdapter) test;
102			return adapter.getDescription();
103		} else if (test instanceof TestDecorator) {
104			TestDecorator decorator= (TestDecorator) test;
105			return makeDescription(decorator.getTest());
106		} else {
107			// This is the best we can do in this case
108			return Description.createSuiteDescription(test.getClass());
109		}
110	}
111}
112
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger getDescription code on LambdaTest Cloud Grid

Execute automation tests with getDescription on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)