Spring Boot CLI - Testing Application


Advertisements

In this chapter, we will test the sample project created in Hello World Example Chapter to demonstrate the testing capabilities of Spring CLI. Follow the steps listed in the table below to test the sample project −

Sr.No Step & Description
1 Create FirstApplication.groovy and TestFirstApplication.groovy in Test folder as explained below.
2 Compile and run the application to verify the result of the implemented logic.

FirstApplication/FirstApplication.groovy

@RestController
class FirstApplication {
   @RequestMapping("/")
   
   String welcome() {
      "Welcome to Howcodex.Com"
   }
} 

FirstApplication/TestFirstApplication.groovy

class TestFirstApplication {
   @Test
   void welcomeTest() {
      assertEquals("Welcome to Howcodex.Com", new FirstApplication().welcome())
   }
} 

Run the application

To run the application, type the following command −

E:/Test/FirstApplication/> spring test FirstApplication.groovy TestFirstApplication.groovy

Now Spring Boot CLI will come into action, download the required dependencies, compile the source and test file and unit test the code. The following output will be generated on console −

Resolving dependencies........................................................
.
Time: 0.457

OK (1 test)

Important points

Consider the following points to understand the actions taken by Spring CLI −

  • The @Test annotation directs CLI to download JUnit 4.12 version.

  • Spring CLI automatically detects the version using its metadata, as we have not specified any dependency.

  • Finally, after code compilation, test the application.

Advertisements