https://github.com/laech/java-stacksrc
Decorates stack traces with source code snippets.
https://github.com/laech/java-stacksrc
gradle java junit maven testng
Last synced: 10 months ago
JSON representation
Decorates stack traces with source code snippets.
- Host: GitHub
- URL: https://github.com/laech/java-stacksrc
- Owner: laech
- License: apache-2.0
- Created: 2020-09-09T10:11:38.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-12-07T02:20:29.000Z (about 2 years ago)
- Last Synced: 2023-12-07T23:30:53.575Z (about 2 years ago)
- Topics: gradle, java, junit, maven, testng
- Language: Java
- Homepage:
- Size: 648 KB
- Stars: 239
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license
Awesome Lists containing this project
- awesome-java - StackSrc
README
# Overview

The goal of this project is to decorate stack traces of test failures to make
them more useful.
So instead of getting this when a test fails:
```
org.opentest4j.AssertionFailedError: expected: but was:
at org.junit...
at com.example.MyTest.hello(MyTest.java:24)
...
```
You'll get this:
```
org.opentest4j.AssertionFailedError: expected: but was:
at org.junit...
at com.example.MyTest.hello(MyTest.java:24)
22 @Test
23 void compareStrings() {
-> 24 assertEquals("hello", "hi");
25 }
...
```
In both your IDE and build server test reports. Also works for other JVM
languages.
## Usage
Requires Java 11.
### Gradle
```groovy
dependencies {
// For JUnit 5:
testImplementation("nz.lae.stacksrc:stacksrc-junit5:${stacksrc.version}")
// For JUnit 4:
testImplementation("nz.lae.stacksrc:stacksrc-junit4:${stacksrc.version}")
// For TestNG:
testImplementation("nz.lae.stacksrc:stacksrc-testng:${stacksrc.version}")
}
```
### Maven
```xml
nz.lae.stacksrc
stacksrc-junit5
stacksrc-junit4
stacksrc-testng
${stacksrc.version}
test
```
### JUnit 5
For JUnit 5, you can
[enable automatic extension detection](https://junit.org/junit5/docs/current/user-guide/#extensions-registration-automatic-enabling)
by setting the system
property `junit.jupiter.extensions.autodetection.enabled=true` on your build,
then no other change will be needed for this to work.
If you don't want to enable automatic extension detection, you can wire things
up manually using either JUnit's `@ExtendWith` or `@RegisterExtension` like the
following, then all tests inheriting `BaseTest` will have their stack traces
decorated on failure:
```java
@ExtendWith(ErrorDecorator.class)
class BaseTest {
}
class MyTest extends BaseTest {
@Test
void myTest() {
// ...
}
}
```
### JUnit 4
For JUnit 4, create a base test with a test rule, then all tests
inheriting `BaseTest` will have their stack traces
decorated on failure:
```java
public class BaseTest {
@Rule
public final ErrorDecorator errorDecorator = new ErrorDecorator();
}
public final class MyTest extends BaseTest {
@Test
public void myTest() {
// ...
}
}
```
### TestNG
No extra work is needed apart from adding the dependency.