[JAVA] Let's create JUnit.


Have you ever heard the phrase, "To study something, it's best to make it." Even though I used JUnit for business, I didn't know how it was made. Since I left the company and am in the process of changing jobs, I have time, so I decided to actually make my own version of JUnit.

JUnit version to refer to

I searched for the JUnit source code, but found only JUnit 4 and JUnit 5. (Other than official, there are other than JUnit4 and JUnit5.) There are two directories under `junit4 / src / main / java /` of JUnit4 source code.

  1. junit
  2. org/junit

Looking inside, it seems that different sources are stored. For the time being, I decided to refer to `` `junit```. The reason is as follows.

  1. Close to Kent Beck's "Test Driven Development: By Example"
  2. Simple code

Rough flow of JUnit

How did you investigate

At first, I read the junit source one by one, but for the following reasons, I just grasped the main flow.

  1. There is a decent amount of classes.
  2. I wanted a tool to organize class relationships, but I didn't want it.
  3. It's a pain if you just read it all the time.

Run JUnit

JUnit is executed according to the following flow. If you use it for business, the integrated development environment will do it for you. Please see JUnit's Official Page for details.

  1. Create a test class.
  2. Compile the test class.
  3. Run TestRunner.

The explanation of how to create a test class and how to compile a test class is omitted.

Run TestRunner.

Execute the following command.

java -cp A class that runs the required library tests(JUnit3 junit.textui.TestRunner)Tested class

The important thing is to specify the class to execute the test and the class to be tested.

JUnit analysis

How is JUnit doing the work? Is there any special mechanism? The bottom line is that you're not doing anything difficult, you're just using Reflection.

Take a look at the entry point

You know that class is the entry point because you have a class that runs the tests. If you actually look at `junit4 / src / main / java / junit / textui / TestRunner.java```, there is a ``` main () method. So junit is simply reading the ``` main () `` method. I'm not doing anything special.

How to execute the test method

Now that you know the entry point, how do you execute the test method? Various methods are called from the `main ()` method earlier, but the ones that actually execute the methods are as follows.

junit4/src/main/java/junit/framework/TestCase.java runTest()Method

Looking at the contents, it's just `method.invoke ()`. (method is Method type) This is a feature of Reflection.

What is Reflection?

Reflection is simply a feature that allows you to read and execute class and method information at runtime. It was a long time ago, but it was famous as black magic. The advantage of Reflection is that you have the flexibility to work with classes and methods. The disadvantage is the flip side of the advantage. Execution speed is slow because the JVM needs to collect information at runtime. You can also execute private methods, so if you use it incorrectly, it will be erratic.

Try to implement

Class structure

I got the minimum knowledge needed to create JUnit above. I thought about the class I needed, assuming that it would perform the minimum functionality.

--Assert (provides assertEquals) --TestResult (hold test result) --TestRunner (entry point) --TestCase (test class superclass)


See GitHub. Currently, the class structure is slightly different.

How was it

The good points to try are as follows.

  1. After a long time of class design, I became a brain teaser.
  2. It's easier to stay motivated than somehow code reading

Recommended Posts

Let's create JUnit.
Let's create Ubuntu environment with vmware
Let's create an instance with .new yourself. .. ..
JUnit 4 notes
JUnit story
Let's create a Java development environment (updating)
[Rails] Let's create a super simple Rails API
JUnit memorandum
Let's create a REST API using WildFly Swarm.
Let's create a timed process with Java Timer! !!
Let's create a RESTful email sending service + client
Let's create a custom tab view in SwiftUI 2.0
Let's create a super-simple web framework in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
Let's quickly create an app that uses Firestore! !!
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]