How to use getTrimmedTrace method of org.junit.runner.notification.Failure class

Best junit code snippet using org.junit.runner.notification.Failure.getTrimmedTrace

Run junit automation tests on LambdaTest cloud grid

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

copy
1public class org.junit.runner.notification.Failure implements java.io.Serializable {
2  public org.junit.runner.notification.Failure(org.junit.runner.Description, java.lang.Throwable);
3  public java.lang.String getTestHeader();
4  public org.junit.runner.Description getDescription();
5  public java.lang.Throwable getException();
6  public java.lang.String toString();
7  public java.lang.String getTrace();
8  public java.lang.String getTrimmedTrace();
9  public java.lang.String getMessage();
10}
11
Full Screen
copy
1package com.neosoft;
2
3import org.junit.runner.JUnitCore;
4import org.junit.runner.Result;
5import org.junit.runner.notification.Failure;
6
7public class TestRunner {
8
9	public static void main(String[] args) {
10
11		Result result = JUnitCore.runClasses(LifeCycleCallBack.class);
12
13		System.out.println("Total Failures: " + result.getFailureCount());
14
15		for (Failure failure : result.getFailures())
16			System.out.println(failure.getTrimmedTrace());
17
18		System.out.println("Run Count: " + result.getRunCount());
19		System.out.println("Result: " + result.wasSuccessful());
20	}
21
22}
Full Screen
copy
1package org.junit.internal;
2
3import java.io.PrintStream;
4import java.text.NumberFormat;
5import java.util.List;
6
7import org.junit.runner.Description;
8import org.junit.runner.Result;
9import org.junit.runner.notification.Failure;
10import org.junit.runner.notification.RunListener;
11
12public class TextListener extends RunListener {
13
14    private final PrintStream writer;
15
16    public TextListener(JUnitSystem system) {
17        this(system.out());
18    }
19
20    public TextListener(PrintStream writer) {
21        this.writer = writer;
22    }
23
24    @Override
25    public void testRunFinished(Result result) {
26        printHeader(result.getRunTime());
27        printFailures(result);
28        printFooter(result);
29    }
30
31    @Override
32    public void testStarted(Description description) {
33        writer.append('.');
34    }
35
36    @Override
37    public void testFailure(Failure failure) {
38        writer.append('E');
39    }
40
41    @Override
42    public void testIgnored(Description description) {
43        writer.append('I');
44    }
45
46    /*
47      * Internal methods
48      */
49
50    private PrintStream getWriter() {
51        return writer;
52    }
53
54    protected void printHeader(long runTime) {
55        getWriter().println();
56        getWriter().println("Time: " + elapsedTimeAsString(runTime));
57    }
58
59    protected void printFailures(Result result) {
60        List<Failure> failures = result.getFailures();
61        if (failures.isEmpty()) {
62            return;
63        }
64        if (failures.size() == 1) {
65            getWriter().println("There was " + failures.size() + " failure:");
66        } else {
67            getWriter().println("There were " + failures.size() + " failures:");
68        }
69        int i = 1;
70        for (Failure each : failures) {
71            printFailure(each, "" + i++);
72        }
73    }
74
75    protected void printFailure(Failure each, String prefix) {
76        getWriter().println(prefix + ") " + each.getTestHeader());
77        getWriter().print(each.getTrimmedTrace());
78    }
79
80    protected void printFooter(Result result) {
81        if (result.wasSuccessful()) {
82            getWriter().println();
83            getWriter().print("OK");
84            getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")");
85
86        } else {
87            getWriter().println();
88            getWriter().println("FAILURES!!!");
89            getWriter().println("Tests run: " + result.getRunCount() + ",  Failures: " + result.getFailureCount());
90        }
91        getWriter().println();
92    }
93
94    /**
95     * Returns the formatted string of the elapsed time. Duplicated from
96     * BaseTestRunner. Fix it.
97     */
98    protected String elapsedTimeAsString(long runTime) {
99        return NumberFormat.getInstance().format((double) runTime / 1000);
100    }
101}
102
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 getTrimmedTrace code on LambdaTest Cloud Grid

Execute automation tests with getTrimmedTrace 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)