The procedure when ** JUnit Test ** is performed in ** IntelliJ IDEA **, which is an integrated development environment, is summarized.
** 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 **.
Right-click with the project file `` `.iml``` file you are currently working on selected
new
--> directory
In this example, the directory name is `` `Test```
File
-->Project Structure
`Modules`
-> `Test``` (test directory created earlier)-> right click->
`Tests```
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.
Place the cursor on the class you want to test (`` Person``` in this example) and select the orange light bulb mark
create test
Select `Testing library`
-> `JUnit4``` (or
`JUnit5```)
Then, a test class with `` `[selected class] Test``` will be generated in the created test directory.
In this example, the PersonTest
class is generated.
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