Best junit code snippet using org.junit.runner.Description.getDisplayName
Source:JUnit4RunListener.java
...65 public void testIgnored( Description description )66 throws Exception67 {68 String reason = getAnnotatedIgnoreValue( description );69 reporter.testSkipped( ignored( getClassName( description ), description.getDisplayName(), reason ) );70 }71 /**72 * Called when a specific test has started.73 *74 * @see org.junit.runner.notification.RunListener#testStarted(org.junit.runner.Description)75 */76 @Override77 public void testStarted( Description description )78 throws Exception79 {80 try81 {82 reporter.testStarting( createReportEntry( description ) );83 }84 finally85 {86 failureFlag.remove();87 }88 }89 /**90 * Called when a specific test has failed.91 *92 * @see org.junit.runner.notification.RunListener#testFailure(org.junit.runner.notification.Failure)93 */94 @Override95 @SuppressWarnings( { "ThrowableResultOfMethodCallIgnored" } )96 public void testFailure( Failure failure )97 throws Exception98 {99 try100 {101 String testHeader = failure.getTestHeader();102 if ( isInsaneJunitNullString( testHeader ) )103 {104 testHeader = "Failure when constructing test";105 }106 String testClassName = getClassName( failure.getDescription() );107 StackTraceWriter stackTrace = createStackTraceWriter( failure );108 ReportEntry report = withException( testClassName, testHeader, stackTrace );109 if ( failure.getException() instanceof AssertionError )110 {111 reporter.testFailed( report );112 }113 else114 {115 reporter.testError( report );116 }117 }118 finally119 {120 failureFlag.set( true );121 }122 }123 @SuppressWarnings( "UnusedDeclaration" )124 public void testAssumptionFailure( Failure failure )125 {126 try127 {128 Description desc = failure.getDescription();129 String test = getClassName( desc );130 reporter.testAssumptionFailure( assumption( test, desc.getDisplayName(), failure.getMessage() ) );131 }132 finally133 {134 failureFlag.set( true );135 }136 }137 /**138 * Called after a specific test has finished.139 *140 * @see org.junit.runner.notification.RunListener#testFinished(org.junit.runner.Description)141 */142 @Override143 public void testFinished( Description description )144 throws Exception145 {146 Boolean failure = failureFlag.get();147 if ( failure == null )148 {149 reporter.testSucceeded( createReportEntry( description ) );150 }151 }152 /**153 * Delegates to {@link RunListener#testExecutionSkippedByUser()}.154 */155 public void testExecutionSkippedByUser()156 {157 reporter.testExecutionSkippedByUser();158 }159 private String getClassName( Description description )160 {161 String name = extractDescriptionClassName( description );162 if ( name == null || isInsaneJunitNullString( name ) )163 {164 // This can happen upon early failures (class instantiation error etc)165 Description subDescription = description.getChildren().get( 0 );166 if ( subDescription != null )167 {168 name = extractDescriptionClassName( subDescription );169 }170 if ( name == null )171 {172 name = "Test Instantiation Error";173 }174 }175 return name;176 }177 protected StackTraceWriter createStackTraceWriter( Failure failure )178 {179 return new JUnit4StackTraceWriter( failure );180 }181 protected SimpleReportEntry createReportEntry( Description description )182 {183 return new SimpleReportEntry( getClassName( description ), description.getDisplayName() );184 }185 protected String extractDescriptionClassName( Description description )186 {187 return extractClassName( description.getDisplayName() );188 }189 protected String extractDescriptionMethodName( Description description )190 {191 return extractMethodName( description.getDisplayName() );192 }193 public static void rethrowAnyTestMechanismFailures( Result run )194 throws TestSetFailedException195 {196 for ( Failure failure : run.getFailures() )197 {198 if ( isFailureInsideJUnitItself( failure.getDescription() ) )199 {200 throw new TestSetFailedException( failure.getTestHeader() + " :: " + failure.getMessage(),201 failure.getException() );202 }203 }204 }205 private static boolean isInsaneJunitNullString( String value )...
Source:SortableTest.java
...15public class SortableTest {16 private static Comparator<Description> forward() {17 return new Comparator<Description>() {18 public int compare(Description o1, Description o2) {19 return o1.getDisplayName().compareTo(o2.getDisplayName());20 }21 };22 }23 private static Comparator<Description> backward() {24 return new Comparator<Description>() {25 public int compare(Description o1, Description o2) {26 return o2.getDisplayName().compareTo(o1.getDisplayName());27 }28 };29 }30 public static class TestClassRunnerIsSortable {31 private static String log = "";32 public static class SortMe {33 @Test34 public void a() {35 log += "a";36 }37 @Test38 public void b() {39 log += "b";40 }...
Source:LogRunListener.java
...14 Log.i("TestRunner", String.format("run finished: %d tests, %d failed, %d ignored", Integer.valueOf(result.getRunCount()), Integer.valueOf(result.getFailureCount()), Integer.valueOf(result.getIgnoreCount())));15 }16 @Override // org.junit.runner.notification.RunListener17 public void testStarted(Description description) throws Exception {18 String valueOf = String.valueOf(description.getDisplayName());19 Log.i("TestRunner", valueOf.length() != 0 ? "started: ".concat(valueOf) : new String("started: "));20 }21 @Override // org.junit.runner.notification.RunListener22 public void testFinished(Description description) throws Exception {23 String valueOf = String.valueOf(description.getDisplayName());24 Log.i("TestRunner", valueOf.length() != 0 ? "finished: ".concat(valueOf) : new String("finished: "));25 }26 @Override // org.junit.runner.notification.RunListener27 public void testFailure(Failure failure) throws Exception {28 String valueOf = String.valueOf(failure.getDescription().getDisplayName());29 Log.e("TestRunner", valueOf.length() != 0 ? "failed: ".concat(valueOf) : new String("failed: "));30 Log.e("TestRunner", "----- begin exception -----");31 Log.e("TestRunner", failure.getTrace());32 Log.e("TestRunner", "----- end exception -----");33 }34 @Override // org.junit.runner.notification.RunListener35 public void testAssumptionFailure(Failure failure) {36 String valueOf = String.valueOf(failure.getDescription().getDisplayName());37 Log.e("TestRunner", valueOf.length() != 0 ? "assumption failed: ".concat(valueOf) : new String("assumption failed: "));38 Log.e("TestRunner", "----- begin exception -----");39 Log.e("TestRunner", failure.getTrace());40 Log.e("TestRunner", "----- end exception -----");41 }42 @Override // org.junit.runner.notification.RunListener43 public void testIgnored(Description description) throws Exception {44 String valueOf = String.valueOf(description.getDisplayName());45 Log.i("TestRunner", valueOf.length() != 0 ? "ignored: ".concat(valueOf) : new String("ignored: "));46 }47}...
Source:UnitTestsScratch.java
...24 final JUnit4TestAdapter test = new JUnit4TestAdapter(clazz);25 final Description testDesc = test.getDescription();26 final ArrayList<TestItem> testItems = new ArrayList<TestItem>();27 testDesc.getChildren().stream().forEach((child) -> {28 testItems.add(TestItem.of(child.getDisplayName(), child.getMethodName()));29 });30 return testItems;31 }32 /** run test method in the test class. */33 public Result runTest(Class<?> clazz, String testMethod) {34 final JUnitCore runner = new JUnitCore();35 runner.addListener(new RunListener() {36 @Override37 public void testRunStarted(Description description) throws Exception {38 log.info("testRun started: {}", description.getDisplayName());39 }40 @Override41 public void testRunFinished(Result result) throws Exception {42 log.info("testRun finished: {}", result.toString());43 }44 @Override45 public void testStarted(Description description) throws Exception {46 log.info("test started: {}", description.getDisplayName());47 }48 @Override49 public void testFinished(Description description) throws Exception {50 log.info("test finished: {}", description.getDisplayName());51 }52 @Override53 public void testFailure(Failure failure) throws Exception {54 log.info("test failure: {}", failure.getMessage());55 }56 });57 return runner.run(FilterRequest.aClass(clazz).filterWith(new Filter() {58 @Override59 public boolean shouldRun(Description description) {60 return testMethod == null || testMethod.equals(description.getMethodName());61 }62 @Override63 public String describe() {64 return String.format("Test %s", testMethod);...
Source:TestRunner.java
...24 JUnitCore junit = new JUnitCore();25 junit.addListener(new RunListener() {26 @Override27 public void testStarted(Description description) {28 System.out.println("\nSTARTING: " + description.getDisplayName());29 }30 @Override31 public void testFailure(Failure failure) {32 System.out.println("FAILED: "33 + failure.getDescription().getDisplayName());34 System.out.println(failure.getTrace());35 }36 });37 Result result = junit.run(CsvParserTest.class,38 ErrorReporterTest.class,39 OptionsTest.class,40 PositionTest.class);41 if (!result.wasSuccessful()) {42 System.out.println("\n*** FAILED ***");43 }44 }45}...
Source:RunAClassWithListener.java
...12 System.out.println("RUN STARTED");13 }14 @Override15 public void testStarted(Description description) throws Exception {16 System.out.println("TEST STARTED: " + description.getDisplayName());17 }18 @Override19 public void testFailure(Failure failure) throws Exception {20 System.out.println("FAILURE: " + failure.getMessage());21 }22 @Override23 public void testIgnored(Description description) throws Exception {24 System.out.println("TEST IGNORED: " + description.getDisplayName());25 }26 @Override27 public void testFinished(Description description) throws Exception {28 System.out.println("TEST FINISHED: " + description.getDisplayName());29 }30 @Override31 public void testRunFinished(Result result) throws Exception {32 System.out.println("RUN FINISHED");33 System.out.println(String.format("| IGNORED: %d", result.getIgnoreCount()));34 System.out.println(String.format("| FAILURES: %d", result.getFailureCount()));35 System.out.println(String.format("| RUN: %d", result.getRunCount()));36 }37 } );38 core.run(MathOperationTest.class);39 }40}...
Source:JournalingListener.java
...17 @Override public void testRunFinished(Result result) throws Exception {18 throw new AssertionError();19 }20 @Override public void testStarted(Description description) throws Exception {21 journal.add("START " + description.getDisplayName());22 }23 @Override public void testFinished(Description description) throws Exception {24 journal.add("FINISH " + description.getDisplayName());25 }26 @Override public void testFailure(Failure failure) throws Exception {27 journal.add(28 "FAIL " + failure.getDescription().getDisplayName() + " " + failure.getMessage());29 }30 @Override public void testAssumptionFailure(Failure failure) {31 journal.add("ASSUMPTION FAIL "32 + failure.getDescription().getDisplayName()33 + " "34 + failure.getMessage());35 }36 @Override public void testIgnored(Description description) throws Exception {37 journal.add("IGNORE " + description.getDisplayName());38 }39 });40 }41 RunNotifier notifier() {42 return notifier;43 }44 List<String> journal() {45 return new ArrayList<>(journal);46 }47}...
Source:JUnitTestHarnessRunNotifier.java
...1617 @Override18 public void fireTestStarted(Description description) throws StoppedByUserException {19 super.fireTestStarted(description);20 started.add(description.getDisplayName());21 }2223 @Override24 public void fireTestFailure(Failure failure) {25 super.fireTestFailure(failure);26 failures.add(failure.getDescription().getDisplayName());27 }2829 @Override30 public void fireTestAssumptionFailed(Failure failure) {31 super.fireTestAssumptionFailed(failure);32 failures.add(failure.getDescription().getDisplayName());33 }3435 @Override36 public void fireTestIgnored(Description description) {37 super.fireTestIgnored(description);38 ignored.add(description.getDisplayName());39 }4041 @Override42 public void fireTestFinished(Description description) {43 super.fireTestFinished(description);44 finished.add(description.getDisplayName());45 }4647 public boolean wasSuccessful() {48 return started.size() == finished.size() && failures.isEmpty();49 }50}
...
getDisplayName
Using AI Code Generation
1import org.junit.runner.Description;2import org.junit.runner.notification.Failure;3import org.junit.runner.notification.RunListener;4public class CustomRunListener extends RunListener {5 public void testRunStarted(Description description) throws Exception {6 System.out.println("Number of tests to execute: " + description.testCount());7 }8 public void testRunFinished(Result result) throws Exception {9 System.out.println("Number of tests executed: " + result.getRunCount());10 }11 public void testStarted(Description description) throws Exception {12 System.out.println("Starting test: " + description.getDisplayName());13 }14 public void testFinished(Description description) throws Exception {15 System.out.println("Finished test: " + description.getDisplayName());16 }17 public void testFailure(Failure failure) throws Exception {18 System.out.println("Test failed: " + failure.getDescription().getDisplayName());19 }20 public void testIgnored(Description description) throws Exception {21 System.out.println("Test ignored: " + description.getDisplayName());22 }23}24import org.junit.runner.Description;25import org.junit.runner.notification.Failure;26import org.junit.runner.notification.RunListener;27public class CustomRunListener extends RunListener {28 public void testRunStarted(Description description) throws Exception {29 System.out.println("Number of tests to execute: " + description.testCount());30 }31 public void testRunFinished(Result result) throws Exception {32 System.out.println("Number of tests executed: " + result.getRunCount());33 }34 public void testStarted(Description description) throws Exception {35 System.out.println("Starting test: " + description.getClassName());36 }37 public void testFinished(Description description) throws Exception {38 System.out.println("Finished test: " + description.getClassName());39 }40 public void testFailure(Failure failure) throws Exception {
getDisplayName
Using AI Code Generation
1public void testGetDisplayName() {2 Description description = Description.createTestDescription(3 "org.junit.runner.DescriptionTest", "testGetDisplayName");4 assertEquals("testGetDisplayName(org.junit.runner.DescriptionTest)", description.getDisplayName());5}6public void testGetClassName() {7 Description description = Description.createTestDescription(8 "org.junit.runner.DescriptionTest", "testGetClassName");9 assertEquals("org.junit.runner.DescriptionTest", description.getClassName());10}11public void testGetMethodName() {12 Description description = Description.createTestDescription(13 "org.junit.runner.DescriptionTest", "testGetMethodName");14 assertEquals("testGetMethodName", description.getMethodName());15}16public void testGetTestClass() {17 Description description = Description.createTestDescription(18 "org.junit.runner.DescriptionTest", "testGetTestClass");19 assertEquals(DescriptionTest.class, description.getTestClass());20}21public void testGetChildren() {22 Description description = Description.createTestDescription(23 "org.junit.runner.DescriptionTest", "testGetChildren");24 assertEquals(0, description.getChildren().size());25}26public void testIsTest() {27 Description description = Description.createTestDescription(28 "org.junit.runner.DescriptionTest", "testIsTest");29 assertTrue(description.isTest());30}31public void testIsSuite() {32 Description description = Description.createSuiteDescription(33 "org.junit.runner.DescriptionTest");34 assertTrue(description.isSuite());35}36public void testIsEmpty() {37 Description description = Description.createSuiteDescription(
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!!