Hi all, from this post I’m going to talk about JUnit testing, which is an unit testing technique using for JAVA. Here I’m talking about how to create a JUnit test for a JAVA class using the InteliJ IDEA IDE.
- First of all you can create a JAVA class.
- Then place the cursor in front of the class declaration and hit ‘Alt + Enter‘.
- From the pop up dialog choose ‘Create Test‘.
- This window will show all the attributes for the particular JUnit. Here I’m using the JUnit4 as the testing library. Then from the below set of methods, choose methods which you want to have test methods.
- Then it will generate a new Test class as below.
This is how to create a test class in JAVA with JUnit. Now let’s see how to write a test case and how to execute it. Something important is, the method you are going to check should not be a ‘void‘ method. It must ‘return‘ something.
- I’m using another class for this purpose with a method which provide a basic functionality of multiplying two numbers and returning the output.
- Here is my test class. Here assertEquals is the method I’m using to check the output with the expected output. 0 is the expected output and the next parameter is the method I’m going to test.
- Now let’s run the test class.
- If the expected output and the actual output is same, then it will pass the test case.
- If the actual output and the expected output is different, then the test case will be failed.
This is a very quick tutorial showing how to use JUnit testing with your JAVA classes and how to create a Test class in JUnit using the InteliJ IDEA IDE. Hope now you have a clear idea about these things. Thank you!