|

Enhance Developer Productivity With Java-Stacksrc Plugin.

In this blog post, we will explore the Java-Stacksrc JUnit 5 Plugin, which is a tool that decorates stack traces with source code snippets. This plugin is particularly useful for enhancing the readability and effectiveness of automated tests written in Java. We will delve into the features and benefits of using this plugin, as well as provide insights into how it can be integrated into your Java development workflow.

Understanding Java-Stacksrc Plugin

Java-Stacksrc is a potent tool that aids in tracing the source of Java stack traces quickly and efficiently in case of test failures. This plugin is designed to improve the debugging process for Java developers. It simplifies the process of identifying the origin of exceptions and errors by providing a direct linkage to the source code responsible for the stack trace.

Features of Java-Stacksrc Plugin

Direct Navigation: Java-Stacksrc plugin provides a direct path to the source code from the stack trace, saving developers from the time-consuming task of manually searching for the source of the problem.

Quick Debugging: With this plugin, you can navigate directly from the console to the source code responsible for the exception, accelerating the debugging process

Enhanced Productivity: By providing quick access to problem areas, Java-Stacksrc can significantly increase a developer’s productivity.

Easy Installation: Java-Stacksrc plugin can be easily installed and integrated with most Java-based development environments

Using Java-Stacksrc Plugin

Adding Dependency

Maven

JUnit5
       <dependency>
            <groupId>nz.lae.stacksrc</groupId>
            <artifactId>stacksrc-junit5</artifactId>
            <version>0.6.0</version>
        </dependency>Code language: Java (java)
JUnit4
       <dependency>
            <groupId>nz.lae.stacksrc</groupId>
            <artifactId>stacksrc-junit4</artifactId>
            <version>0.6.0</version>
        </dependency>Code language: Java (java)
TestNG
       <dependency>
            <groupId>nz.lae.stacksrc</groupId>
            <artifactId>stacksrc-testng</artifactId>
            <version>0.6.0</version>
        </dependency>Code language: Java (java)

Gradle

JUnit5
      dependencies { 
            testImplementation("nz.lae.stacksrc:stacksrc-junit5:0.6.0")
       }Code language: Java (java)
JUnit4
      dependencies { 
            testImplementation("nz.lae.stacksrc:stacksrc-junit4:0.6.0")
       }Code language: Java (java)
TestNG
      dependencies { 
            testImplementation("nz.lae.stacksrc:stacksrc-testng:0.6.0")
       }Code language: Java (java)

Using in your Tests

JUnit 5

For JUnit 5, you can use the plugin with @ExtendWith annotation.

@ExtendWith(ErrorDecorator.class)
public class EmployeeTest {

    Employee emp = new Employee().getEmployee();

    @Test
    public void TestGetEmployee() {

        assertEquals("Alex",emp.getFirst_name());
        assertEquals("Smith",emp.getLast_name());
        assertEquals(Gender.F,emp.getGender());
        assertEquals("Historical Society of Victoria",emp.getAddressLine1());
        assertEquals("239 A'Beckett Street",emp.getAddressLine2());
        assertEquals("Melbourne",emp.getCity());
        assertEquals("Australia",emp.getCountry());

    }Code language: Java (java)

JUnit 4

For JUnit 4,You can enable plugin with @Rule annotation


public class EmployeeTest {
  @Rule
  public final ErrorDecorator errorDecorator = new ErrorDecorator();

    Employee emp = new Employee().getEmployee();

    @Test
    public void TestGetEmployee() {

        assertEquals("Alex",emp.getFirst_name());
        assertEquals("Smith",emp.getLast_name());
        assertEquals(Gender.F,emp.getGender());
        assertEquals("Historical Society of Victoria",emp.getAddressLine1());
        assertEquals("239 A'Beckett Street",emp.getAddressLine2());
        assertEquals("Melbourne",emp.getCity());
        assertEquals("Australia",emp.getCountry());

    }Code language: Java (java)

Running Tests

Let’s now run the tests and with and without plugin enabled and see the difference

Running Tests Without plugin

In below console log, you can see that error line show where test failed, you have to specifically look for it.

[INFO] --- maven-surefire-plugin:3.2.1:test (default-test) @ java-stacksrc-demo ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running dev.fullstackcode.stacksrc.demo.test.EmployeeTest
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.194 s <<< FAILURE! -- in dev.fullstackcode.stacksrc.demo.test.EmployeeTest
[ERROR] dev.fullstackcode.stacksrc.demo.test.EmployeeTest.TestGetEmployee -- Time elapsed: 0.105 s <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <Historical Society of Victoria> but was: <Royal Historical Society of Victoria,>
        at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1145)
        at dev.fullstackcode.stacksrc.demo.test.EmployeeTest.TestGetEmployee(EmployeeTest.java:25)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

[INFO] 
[INFO] Results:
[INFO]
[ERROR] Failures: 
[ERROR]   EmployeeTest.TestGetEmployee:25 expected: <Historical Society of Victoria> but was: <Royal Historical Society of Victoria,>
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
Code language: Java (java)

Running Tests With plugin

In below console log, you can see that error line and also the corresponding source line which failed the assertion.

[INFO] --- maven-surefire-plugin:3.2.1:test (default-test) @ java-stacksrc-demo ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running dev.fullstackcode.stacksrc.demo.test.EmployeeTest
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.227 s <<< FAILURE! -- in dev.fullstackcode.stacksrc.demo.test.EmployeeTest
[ERROR] dev.fullstackcode.stacksrc.demo.test.EmployeeTest.TestGetEmployee -- Time elapsed: 0.156 s <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <Historical Society of Victoria> but was: <Royal Historical Society of Victoria,>
        at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1145)
        at dev.fullstackcode.stacksrc.demo.test.EmployeeTest.TestGetEmployee(EmployeeTest.java:25)

           23          assertEquals("Smith",emp.getLast_name());
           24          assertEquals(Gender.F,emp.getGender());
        -> 25          assertEquals("Historical Society of Victoria",emp.getAddressLine1());
           26          assertEquals("239 A'Beckett Street",emp.getAddressLine2());
           27          assertEquals("Melbourne",emp.getCity());




[INFO] 
[INFO] Results:
[INFO]
[ERROR] Failures: 
[ERROR]   EmployeeTest.TestGetEmployee expected: <Historical Society of Victoria> but was: <Royal Historical Society of Victoria,>
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
Code language: Java (java)

Conclusion

The Java-Stacksrc JUnit 5 Plugin is a valuable tool for Java developers seeking to enhance the effectiveness and readability of their automated tests. By seamlessly integrating with JUnit 5 and providing enhanced stack trace decoration, this plugin can streamline the process of identifying and addressing issues in test code. Whether you are working on a small project or a large-scale application, the Java-Stacksrc JUnit 5 Plugin can be a valuable addition to your testing toolkit. In conclusion, the Java-Stacksrc JUnit 5 Plugin offers a powerful solution for improving the testing experience in Java development, and its integration with JUnit 5 makes it a compelling choice for developers looking to elevate the quality of their automated

You can download the source code for the blog post from GitHub

Similar Posts