JUNIT TESTING

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.

j1.png

  • Then place the cursor in front of the class declaration and hit ‘Alt + Enter‘.

j2

  • From the pop up dialog choose ‘Create Test‘.

j3.png

  • 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.

j4

  • Then it will generate a new Test class as below.

j5.png

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.

j7

  • 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.

j6.png

  • Now let’s run the test class.

j8.png

  • If the expected output and the actual output is same, then it will pass the test case.

j9.png

  • If the actual output and the expected output is different, then the test case will be failed.

j10

j11

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!

 

Advertisements

2 thoughts on “JUNIT TESTING

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s