Spring Boot Starter TestThe spring-boot-starter-test is the primary dependency for the test. It contains the majority of elements required for our tests. There are several different types of tests that we can write to help test and automate the health of an application. Before starting any testing, we need to integrate the testing framework. With Spring Boot, we need to add starter to our project, for testing we only need to add the spring-boot-starter-test dependency. It pulls all the dependencies related to test. After adding it, we can build up a simple unit test. We can either create the Spring Boot project through IDE or generate it using Spring Initializr. Note: If you are adding test dependency manually, add it to the bottom of the pom.xml file.In the above dependency, one thing to be noticed that it includes the scope of test <scope>test</scope>. It means when the application is bundled and packaged for deployment, any dependency that is declared with the test scopes is ignored. The test scope dependencies are only available when running in the development and Maven test modes. When we create a simple Spring Boot application, by default, it contains the test dependency in the pom.xml file and ApplicationNameTest.java file under in the folder src/test/java. Let's create a simple maven project. Spring Boot Starter Test ExampleStep 1: Open Spring Initializr https://start.spring.io/. Step 2: Provide the Group name and Artifact Id. We have provided Group name com.javatpoint and Artifact spring-boot-test-example. Step 3: Add the Spring Web dependency. Step 4: Click on the Generate button. When we click on the Generate button, it wraps all the specifications related to the project and downloads a Jar file to our local system. Step 5: Extract the downloaded Jar file. Step 6: Import the folder to STS. It takes some time to import. File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-test-example -> Finish After importing the project, we can see the following project directory in the Package Explorer section of the STS. We can see in the above directory that it contains a test file named SpringBootTestExampleApplicationTest.java in the folder src/test/java. SpringBootTestExampleApplicationTest.java The above code implements two annotation by default: @SpringBootTest, and @Test.
Step 7: Open the SpringBootTestExampleApplicationTest.java file and run it as Junit Test. When we run the above code, it displays the following: Next TopicSpring Boot DevTools |