LoggingTestExtension.java

1
package fr.sii.ogham.testing.extension.junit;
2
3
import java.util.Optional;
4
5
import org.junit.jupiter.api.extension.AfterEachCallback;
6
import org.junit.jupiter.api.extension.BeforeAllCallback;
7
import org.junit.jupiter.api.extension.BeforeEachCallback;
8
import org.junit.jupiter.api.extension.ExtensionContext;
9
import org.junit.jupiter.api.extension.RegisterExtension;
10
import org.junit.platform.commons.support.AnnotationSupport;
11
12
import fr.sii.ogham.testing.extension.common.LogTestInformation;
13
import fr.sii.ogham.testing.extension.common.Printer;
14
import fr.sii.ogham.testing.extension.common.TestInformationLogger;
15
16
/**
17
 * Write information about test. This is useful when there are many tests:
18
 * <ul>
19
 * <li>To quickly find the logs for the test</li>
20
 * <li>To quickly know if the test has failed or succeeded</li>
21
 * <li>To quickly identify the test failure</li>
22
 * <li>To quickly find failed tests</li>
23
 * </ul>
24
 * 
25
 * @author Aurélien Baudet
26
 *
27
 */
28
public class LoggingTestExtension implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback {
29
	private TestInformationLogger logger;
30
31
	/**
32
	 * No logger initialized (it will be initialized using annotation). If you
33
	 * use {@link RegisterExtension} annotation, please use another constructor.
34
	 * 
35
	 */
36
	public LoggingTestExtension() {
37
		super();
38
	}
39
40
	/**
41
	 * Initializes with the provided max line length.
42
	 * 
43
	 * Uses Slf4j logger and default marker ("test-info").
44
	 * 
45
	 * @param maxLength
46
	 *            the length of each line
47
	 */
48
	public LoggingTestExtension(int maxLength) {
49
		super();
50
		this.logger = new TestInformationLogger(maxLength);
51
	}
52
53
	/**
54
	 * Initializes with the provided max line length and marker.
55
	 * 
56
	 * Uses Slf4j logger.
57
	 * 
58
	 * @param maxLength
59
	 *            the length of each line
60
	 * @param marker
61
	 *            the marker for logs
62
	 */
63
	public LoggingTestExtension(int maxLength, String marker) {
64
		super();
65
		this.logger = new TestInformationLogger(maxLength, marker);
66
	}
67
68
	/**
69
	 * 
70
	 * @param maxLength
71
	 *            the length of each line
72
	 * @param marker
73
	 *            the marker for logs
74
	 * @param logger
75
	 *            the logger
76
	 */
77
	public LoggingTestExtension(int maxLength, String marker, Printer logger) {
78
		super();
79
		this.logger = new TestInformationLogger(maxLength, marker, logger);
80
	}
81
82
	@Override
83
	public void beforeAll(ExtensionContext context) throws InstantiationException, IllegalAccessException {
84 1 1. beforeAll : negated conditional → NO_COVERAGE
		if (logger != null) {
85
			return;
86
		}
87
		LogTestInformation annotation = AnnotationSupport.findAnnotation(context.getElement(), LogTestInformation.class).orElse(null);
88 1 1. beforeAll : negated conditional → NO_COVERAGE
		if (annotation == null) {
89
			logger = new TestInformationLogger();
90
		} else {
91
			logger = new TestInformationLogger(annotation.maxLength(), annotation.marker(), annotation.printer().newInstance());
92
		}
93
	}
94
95
	@Override
96
	public void beforeEach(ExtensionContext context) throws Exception {
97 1 1. beforeEach : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeStart → NO_COVERAGE
		logger.writeStart(context.getDisplayName());
98
	}
99
100
	@Override
101
	public void afterEach(ExtensionContext context) throws Exception {
102
		Optional<Throwable> executionException = context.getExecutionException();
103 1 1. afterEach : negated conditional → NO_COVERAGE
		if (executionException.isPresent()) {
104 1 1. afterEach : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeFailure → NO_COVERAGE
			logger.writeFailure(context.getDisplayName(), executionException.get());
105
		} else {
106 1 1. afterEach : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeSuccess → NO_COVERAGE
			logger.writeSuccess(context.getDisplayName());
107
		}
108
	}
109
110
}

Mutations

84

1.1
Location : beforeAll
Killed by : none
negated conditional → NO_COVERAGE

88

1.1
Location : beforeAll
Killed by : none
negated conditional → NO_COVERAGE

97

1.1
Location : beforeEach
Killed by : none
removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeStart → NO_COVERAGE

103

1.1
Location : afterEach
Killed by : none
negated conditional → NO_COVERAGE

104

1.1
Location : afterEach
Killed by : none
removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeFailure → NO_COVERAGE

106

1.1
Location : afterEach
Killed by : none
removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeSuccess → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM