[IntelliJ IDEA] Perform Unit Test

Introduction

The procedure when ** JUnit Test ** is performed in ** IntelliJ IDEA **, which is an integrated development environment, is summarized. intellij-idea-java-ide_orig.jpg

** JUnit Test ** is a test *** that verifies whether the program you created works properly in any case.

For example, even if you prepare an int type variable and wait for standard input with the content "Please enter your favorite number from 1 to 9!", An error will occur if the user tries to input a character string (String type). right!

But when I compiled it, there was no error. .. .. What an experience

Let's test in advance the code that you don't know what will happen with the input data! That's ** JUnit Test **.

procedure

1. Prepare the Java file you want to Junit test

Screen Shot 2019-05-09 at 11.19.13.png This time, ** JUnit Test ** is performed on the `` `Person``` class of this ``` Main.java```.

2. Create a test directory

Screen Shot 2019-05-09 at 9.56.23.png Right-click with the project file `` `.iml``` file you are currently working on selected

new --> directory

Screen Shot 2019-05-09 at 9.56.44.png

In this example, the directory name is `` `Test```

3. Set the created directory for Tests

File-->Project Structure

Screen Shot 2019-05-09 at 9.57.05.png

`Modules`-> `Test``` (test directory created earlier)-> right click-> `Tests```

Screen Shot 2019-05-09 at 9.57.24.png

When set to the ** Tests ** directory, the color of the test directory will change to green as shown on the left in the figure below. Screen Shot 2019-05-09 at 9.58.06.png

4. Create a test class

Place the cursor on the class you want to test (`` Person``` in this example) and select the orange light bulb mark

create test Screen Shot 2019-05-09 at 11.45.33.png

Select `Testing library`-> `JUnit4``` (or `JUnit5```)

Screen Shot 2019-05-09 at 11.46.05.png

Then, a test class with `` `[selected class] Test``` will be generated in the created test directory.

In this example, the PersonTest class is generated.

5. Edit the test class

Screen Shot 2019-05-09 at 10.17.10.png

I edited it like this.

assertEquals(Prospective result,class.function)



 It is described as follows.

 In other words, in this example, we expect the string `` `Shuto``` as the return value of `` `Person.name ()` ``.


## 6. Run
 <img width="931" alt="Screen Shot 2019-05-09 at 10.18.26.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/377231/eefc65e7-a3c7-3389-29f9-961cf24cb14b.png ">
 <img width="530" alt="Screen Shot 2019-05-09 at 10.23.30.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/377231/a190a14c-e313-dc33-37f7-d8a615e8bf51.png ">

```run 'persontest'```

 <img width="803" alt="Screen Shot 2019-05-09 at 13.43.23.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/377231/9a55d013-3acb-c79e-07e2-d4579d54cc2a.png ">

 If you get the expected result, a green line will appear for a moment and `` `Tests passed` `` will appear.

 Now, let's change the expected result to `` `Shuto``` ---> `` `hello``` and retest.

 <img width="1005" alt="Screen Shot 2019-05-09 at 10.27.02.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/377231/fd82b0ed-cdaa-cd55-98a1-91df0c5e44dc.png ">

 The expected result was different from the actual result, so it became ``` Tests fialed``` (should I say it was intentional).

 ```Expected: `` `Expected value
 ```Actual: ``` Actually returned value


 In this way ** JUnit Test ** is done.


 By the way, as shown in the figure below, the parts that have already been tested are shown in green, and the parts that have not been tested or failed are shown in red.

 <img width="662" alt="Screen Shot 2019-05-09 at 14.25.41.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/377231/09a765c1-146b-df5c-6dbf-4f82f75ab485.png ">


 I think that it will be a flow to test the code created like this and improve it one by one.

# in conclusion
 This time, I did ** JUnit Test ** for the created Java class.

 Once you learn how to do it, you can test it very easily.

 Thank you for reading.


Recommended Posts

[IntelliJ IDEA] Perform Unit Test
IntelliJ IDEA settings
Java Unit Test Library-Artery-Sample
Unit test with Junit.
Hot deploy with IntelliJ IDEA
Introduction to Micronaut 2 ~ Unit test ~
Unit test architecture using ArchUnit
Implementation of unit test code
Try using IntelliJ IDEA once
How to unit test Spring AOP
Java Unit Test Library-Artery-ArValidator Validates Objects
Java Unit Test Library-Artery-Current Date Judgment
Recognize Rails projects with Intellij idea
[Rails5] Rspec -Unit test when nesting-
About app testing RSpec (unit test)
Significance of existence of unit test (self-discussion)
Java Unit Test Library-Artery / JUnit4-Array Equivalence
Java + OpenCV 3.X in IntelliJ IDEA
[RSpec] Unit test (using gem: factory_bot)
Let's unit test with [rails] Rspec!