With examples – Spring Boot and TestNG

  • by

I have made example projects that you can add and work in Eclipse IDE to practice and develop Spring Boot with TestNG testing. First download the projects ZIP file given below and open it before you proceed any further.

Download File

OK, now that you have the projects ZIP downloaded to your laptop/desktop. I will walk you through some of topics you need to know to get the projects extended for your need. You first need to know how to add TestNG to your existing project. Then you need to know how to write a basic simple TestNG testcase.

Project Setup – How to setup Spring Boot with TestNG?

If you have an existing Spring Boot maven project then you can simply add TestNG dependency in that project folder. Maven allows dependencies to be mentioned in pom.xml file. You need to add them under dependency tag as shown below.

Press ALT+F5 (shortcut for maven project update) after you add it in your pom.xml file. In case if you have downloaded the projects.zip file above then you have everything added already in the respective file. Maven project update will fetch missing dependency jar files from maven repository to your project environment.

If your local maven repository doesn’t have any of the dependency then the same will be fetched from the remote maven repositories pre-configured in your maven installation.

Writing a basic TestNG test case in Spring Boot

Create a test class to hold your test case. Under src/test/java create your org directory package structure. Create a simple class extending AbstractTestNGSpringContextTests class. Refer to the java class provided in the test folder on the downloaded projects zip file.

Now introduce a new method for your every test cases. Let say you wanna test an API endpoint using TestNG for your new Spring Boot application. You need to specify here details of your API (I mean the API spec). See the below screenshot for more information and reference.

Add a listener to your TestNG

You can also add one or more listener to your TestNG project which you have built now. Listeners are interfaces that modifies the default TestNG’s behavior. As the name suggests Listeners “listen” to the event defined and behave accordingly.

Extending the projects ZIP file

Add more test cases in the same class and check if you could do more than a test case for your project requirement. Write down your comment below if you need any more samples here.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Reply

Welcome to CloudiKnow.com blog, a platform to share knowledge and solution experience related to public cloud is base design of this site.

You can start the cloud journey right from here!
Please bookmark this page by pressing simply CTRL+D.