JUnit Sample Test

Learn how can you write JUnit tests using example JUnit Sample Tests. As we know JUnit is testing framework which is used to create the Unit Test Cases. We will see the example how can you use the JUnit to create the Unit testing test cases.

 

We first need some Java classes to test then we will design the Unit test class for this Java class. Suppose we have the following BaseClass class, which has two methods. one for addition and another for subtraction.

 

Write Unit Test in JUnit

Now we will create the Test class to test this Java class. Let’s see the example of both of the classes.

 

Now, let’s see the JUnit test class for to test the above Java class. In the below Unit test class we have 3 test methods to test add subtract and get value. We have one more method to set the variable value.

 

To execute the Test Class, right click on the Test class. Hover mouse on the Run As option and select the option Junit Test.

Run JUnit Test

 

You can see the execution report in the JUnit console. JUnit have very rich execution reporting. It will show the execution status of every Test method. Test method green means no error in method and red means test method got failed.

JUnit Report

So we have seen, How can we create the Unit Tests for the Java file. We have seen how to declare the Test method and how can we execute the JUnit test Cases. We also have seen the Junit report status.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply