DEV Community

Pradist Keawborisut
Pradist Keawborisut

Posted on

1

Java display test case name when run unit tests (Maven)

ช่วงนี้ได้กลับมาเขียน Java อีกครั้ง โดย project ที่ดูแลอยู่ใช้ maven เป็น dependency management และได้ลองเขียน unit test โดยใช้คำสั่งตามด้านล่าง

mvn test
Enter fullscreen mode Exit fullscreen mode

results ของ unit test จะแสดงตามรูปด้านล่าง

Image description

จากรูปจะพบว่า results ที่ได้จะแสดง summary แบบที่ไม่มีชื่อ test case ที่เราได้เขียนไป แล้วจะทำอย่างไร ที่จะทำให้ผลของ unit test แสดงชื่อ test case ด้วย

ผมไปเจอกับ plugin ตัวหนึ่งชื่อ maven-surefire-plugin plugin ตัวนี้จะแสดงชื่อ test case ที่เราได้เขียนเอาไว้ โดยเพิ่ม code เข้าไปใน pom.xml ตาม code ด้านล่าง

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.2.3</version>
  <dependencies>
    <dependency>
      <groupId>me.fabriciorby</groupId>
      <artifactId>maven-surefire-junit5-tree-reporter</artifactId>
      <version>1.2.1</version>
    </dependency>
  </dependencies>
  <configuration>
    <reportFormat>plain</reportFormat>
  </configuration>
</plugin>
Enter fullscreen mode Exit fullscreen mode

run test mvn test
results ของ unit test จะแสดงตามรูปด้านล่าง

Image description

ตอนนี้จะเห็นได้ว่ามี test case มาแสดงให้เห็นแล้ว

แล้วจะทำอย่างไรให้มันดูง่ายขึ้นกว่านี้ได้อีก?

ไปเจอ maven-surefire-junit5-tree-reporter เป็น extension ของ maven-surefire-plugin โดยมันจะทำให้ test case แสดงในรูปแบบของ tree

เพิ่ม code ด้านล่างไปในส่วนของ configuration ของ plugin maven-surefire-plugin

<configuration>
  <reportFormat>plain</reportFormat>
  <consoleOutputReporter>
    <disable>true</disable>
  </consoleOutputReporter>
  <statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
    <theme>UNICODE</theme>
  </statelessTestsetInfoReporter>
</configuration>
Enter fullscreen mode Exit fullscreen mode

run test mvn test
results ของ unit test จะแสดงตามรูปด้านล่าง

Image description

จากรูปจะเห็นได้ว่า test results นั้นแสดงได้อย่างสวยงาม โดยจะมีสีและ icon บอก status ด้วยว่าแต่ละ test case นั้น success หรือ fail

โดยที่ maven-surefire-junit5-tree-reporter สามารถเลือก theme ได้ด้วย theme ที่ผมเลือกเป็น UNICODE

ถ้าจะลองเล่นไปที่ link นี้ได้เลยครับ

https://github.com/fabriciorby/maven-surefire-junit5-tree-reporter

ตัวอย่าง pom.xml เต็มๆ

หรือใครมีวิธีอื่นอีก แนะนำกันใน comment ได้เลยครับ

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay