A story about having a hard time aligning a testing framework with Java 6

The site is Java 6.

I've been doing manual tests all the time, but there are limits, right? I ended up writing test code and helped with that estimate.

I started by collecting JUnit and Mockito, but I don't know. I had a hard time because it was too old and I couldn't hit the information easily.

You die if you have too much technical debt? A memorandum as a case.

JUnit 4.11 Speaking of writing test code in Java, JUnit. The latest is JUnit 5, but it can only be used with Java 8 or later, so choose from 4 series. It must be 4.8 or later to use the @Rule annotation described later.

assertJ 1.7.1 Assertion library.

Because it is a style of writing by connecting with a method chain It's hard to become ((((parentheses hell)))) You can write a pompon assert statement according to the Eclipse suggestion.

Mockito 1.9.5 A library that makes it fairly easy to implement test mockups. It seems to read as Mojito. For JUit5 series, there is Mockito 2.x.x, but for JUnit4 series, the stability seems to be 1.9.5.

What is a mock?

Suppose you want to test the hogehoge () method of the Hoge class. If the hogehoge () method depends on another Fuga class You can't test without preparing the Fuga class.

When that is the case with a mock "When you see Fuga # fugafuga () from the Hoge class, just return 3." Can be set. It is essential to say test in Java.

PowerMock 1.5.6 Mockito is really useful, but you can't mock static methods. PowerMock hacks it nicely You can mock static methods in a similar way to Mockito. Also private methods.

In the field where Java has been used for a long time, static methods are also available This is also essential because it is terrifying (prejudice).

This Power Mock and Eclemma, which will be described later, were incompatible and I had a hard time. (Solved in the latest version)

I chose 1.5.6 because Mockito 1.9.x and PowerMock 1.5.x are compatible.

Javassist3.18.2 Javassist is a library used when playing with Java bytecode. Java bytecode is the Java source we wrote The one that can be compiled. The one that runs on the JVM.

PowerMock uses this. Java bytecode rather than source It seems that the mock is realized by rewriting it well. (I don't know the details, and this explanation may be incorrect)

Javassist 3.4 is included in the existing project, That was the highest priority, so when I got angry, I was addicted to "What? You're in?"

QuickJUnit Eclipse plugin for writing JUnit. There are various shortcuts and templates.

Eclemma 3.0.1 ** Ecl ** ipse + ** EMMA ** library. A plugin for running EMMA on Eclipse to measure test coverage. Eclemma stopped updating a long time ago and I really wanted to use JaCoCo.

Eclemma and PowerMock 1.5.6 are very incompatible, When I run a test using PowerMock, it shows 0% coverage.

Eclemma looks at the Java bytecode I just described and sees where it was executed. I'm watching PowerMock (to mock static methods) Because (a copy of) that bytecode is edited with Javassist and that is executed.

This problem has been solved for Java8, JUnit5 and later.

Rule annotation

By using it instead of RunWith The above "0% coverage problem" can be avoided.

Reference: https://code.i-harness.com/ja/q/1647e8c

Summary

This is all (I think) I was able to write test code for several methods, execute it, and measure coverage.

It's still good because it's Java, because it's just hard to find, and if you look for it, it will come out.

Why did you have such a hard time?

(1) Multiple problems occurred at the same time.

Javassist version conflict and PowerMock and Eclemma "0% coverage issue"

Indirectly, because the development environment is that the version of Eclipse is too old & there is no Maven Installing plugins and libraries was very troublesome, and there were problems.

Even if it's a trivial problem one by one If multiple occurrences occur, the time required for resolution will increase immediately.

② Information is buried because it has already been resolved

For JUnit 5 and later, the "0% coverage" problem of PowerMock and Eclemma has been solved.

In other words, it was resolved two years ago in the world, so if you go around with those two keywords now, The way to write RunWith that does not use Rule annotation is more hit. (It seems to be annoying to write in Rule)

Serpentine

I had a hard time making it a procedure, but this testing framework group I don't know if it will be adopted as I suggested.

Above all, it is said that "100% coverage is essential", so I have a bad feeling.

Postscript

After all, "Writing a test takes time, so let's do a manual test." I came to the conclusion.

What kind of judgment! I mean, I'm making a manual test case! I hate it! !!

Recommended Posts

A story about having a hard time aligning a testing framework with Java 6
A story about developing ROS called rosjava with java
[Note] A story about changing Java build tools with VS Code
A story about hitting the League Of Legends API with JAVA
A story about Java 11 support for Web services
A story about the JDK in the Java 11 era
The story that docker had a hard time
A story about trying to operate JAVA File
[PHP] A story about outputting PDF with TCPDF + FPDI
A story about trying to get along with Mockito
A story about trying hard to decompile JAR files
A story about reducing memory consumption to 1/100 with find_in_batches
A story about introducing Evolutions into the Play Framework
Story of making a task management application with swing, java
About the behavior when doing a file map with java
A confused story about a ternary operator with multiple conditional expressions
A story about misunderstanding how to use java scanner (memo)
A note about Java GC
Streamline Java testing with Spock
A story stuck with NotSerializableException
A story about using the CoreImage framework to erase stains with Swift and implement a blur erase function
A story that I struggled to challenge a competition professional with Java
A story stuck with log output in Docker + Play framework environment
Learn about transaction savepoints (with Java)
A really scary (Java anti-pattern) story
About signature authentication with java 1st
The story of making a game launcher with automatic loading function [Java]
A story about a Spring Boot project written in Java that supports Kotlin
[Beginner] A story about starting studying Java for job hunting ~ 2nd month ~
[Beginner] A story about starting studying Java for job hunting ~ 3rd month ~
[Beginner] A story about starting studying Java for job hunting ~ 1st month ~
[Beginner] A story about starting studying Java for job hunting ~ 5th month ~
[Beginner] A story about starting studying Java for job hunting ~ 4th month ~