DevLabs Alliance - WhatsApp
DevLabs Alliance Logo

Home / Interview /Step by step tutori...

Step by step tutorial for creating JUnit Tests In Eclipse

Srinivasan G

2024-01-23

DevLabs Alliance Interview

0 mins read

DevLabs Alliance Interview

Steps to install and Creating and Executing Test Cases in JUnit

Start Eclipse

Create new Project folder through the following navigation flow: 


File->New->Others->Maven project. The user is required to enter the Project folder name in a new window that appears. Below are the screenshots for the same:




Click on Maven Project ->Next





Select the Check box as shown in the below screenshot and click Next



Give Group ID

User needs to give Group id Name and Artifact Id -> Finish



Add Dependencies

User needs to add required dependencies like TestNG, Selenium repos to POM.Xml file



Add New JUnit Testcase

Now let us add a new JUNIT Testcase into the project folder.


Select 


Project folder -> src folder -> Right-click on the src folder -> Select New -> Junit Test Case.



Define Folder Path

A new pop-up window will open where we need to enter the following:


  • Browse the source folder path in the Source folder.
  • Enter the package name. The files are placed under the default package if the package name is not entered, which is typically discouraged or, to put it another way, not a good coding practise to adhere to.
  • Enter the JUnit class name.
  • Following are few stub methods: setUpBeforeClass(), tearDownAfterClass(), setUp(), teardown(). If you want these methods added to a ready template, click the appropriate checkbox.
  • Click on Finish button.




Click on -> OK



Generate Default Template

The default template of the class file will get generated as shown below:



Basic Examples

JUnit 4 Test – Basic Examples


Let’s now create a basic JUnit 4 test.


Under the package DLAJUNIT.COM, JUnit test class file has been created and we have included a method test().


This method verifies whether the title variable contains the string that is being passed in the condition.


The comparison of the expected condition is being performed by the specific JUnit method – assertTrue(). This method will check whether the title variable contains some specific word of the

expected condition.

 

@Test annotation is also being added here that defines the test case in a JUnit class file.


Similar to this, by putting many methods in place and giving each one a @Test annotation, you can have multiple test cases in one class file.



Example 1:


Executing the code snippet below should pass the test because the actual string contains the

expected string value.




Now, Right Click on the Script -> RunAs -> Junit Test




On executing the JUnit class, the console and JUnit result tab shows up,


1. The Console displays the message as follows:

‘Beforeclass passed’

‘Verify the title name’

‘Afterclass passed’


2. The JUnit result page shows the total number of test cases run, errors, and failures, i.e. Run: 1/1 (indicating 1 testcase out of 1 testcase ran), Errors: 0 (no errors are found in the test case ran), Failures: 0(no test case got failed)


3. The amount of time required to complete the tests' execution.


4. If all test cases pass, a green bar is shown.


5. You can see various icons on the JUnit tab, just above the timestamp: The first icon shows ‘Next Failed Test’, the second icon shows ‘Previous Failed Test’, and the third icon with a blue and red cross filters out only Failed tests. The icon next to this allows you to only view test cases that were skipped during execution.



Meet The Author

DevLabs Alliance Author

Srinivasan G


HOD Neoload


DevLabs Alliance TwitterDevLabs Alliance LinkedInDevLabs Alliance Instagram

Author Bio

IT industry professional with over 9 years of experience. His expertise lies in architecting and implementing IoT solutions using various Azure services such as IoT Hub, Event Hub, IoT Edge, DPS, Stream Analytics, Storage accounts, Azure Databricks, and Cosmos DB.

INQUIRY

Want To Know More


Email is valid



Phone


By tapping continuing, you agree to our Privacy Policy and Terms & Conditions

“ The hands-on projects helped our team put theory into practice. Thanks to this training, we've achieved seamless collaboration, faster releases, and a more resilient infrastructure. ”
DevLabs Alliance Blogs Page Review
Vijay Saxena

SkillAhead Solutions

Lets get started today!

and get that DREAM JOB

DevLabs Alliance Footer section
DevLabs Alliance LinkedIn ProfileDevLabs Alliance Twitter ProfileDevLabs Alliance Facebook ProfileDevLabs Alliance Facebook Profile
DevLabs Alliance Logo

Gurgaon

USA

1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA

DevLabs Alliance ISO 9001

DevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer Section

`Copyright © DevLabs Alliance. All rights Reserved`

|

Refund & Reschedule Policy

Privacy Policy

Terms of Use