0% found this document useful (0 votes)
50 views

Testing Controller Layer Lecture Notes

The document discusses testing the controller layer of a Spring Boot application. It describes using the @WebMvcTest annotation to load only the controller and its dependencies, avoiding loading the entire application. It provides examples of building REST APIs for employee resources and unit testing them for positive and negative scenarios. Common libraries used in testing like Mockito, Hamcrest, and JsonPath are also summarized.

Uploaded by

Prabhu 4u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Testing Controller Layer Lecture Notes

The document discusses testing the controller layer of a Spring Boot application. It describes using the @WebMvcTest annotation to load only the controller and its dependencies, avoiding loading the entire application. It provides examples of building REST APIs for employee resources and unit testing them for positive and negative scenarios. Common libraries used in testing like Mockito, Hamcrest, and JsonPath are also summarized.

Uploaded by

Prabhu 4u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Spring Boot Applicatio

Controller Layer Testing


By Ramesh Fadatare (Java Guides)

Spring Boot Application


Controller Layer Testing

Controller Service Repository


DB
Layer Layer Layer

Mockito @WebMvcTest

Controller Layer Testin


1.@MockMvcTest Annotation
2.Build createEmployee REST API
3.Unit test createEmployee REST API
4.Build GetAllEmployees REST API
5.Unit test GetAllEmployees REST API
6.Refactoring JUnit test to use static imports
7.Build getEmployeeById REST API
8.Unit test getEmployeeById REST API - Positive Scenario
9.Unit test getEmployeeById REST API - Negative Scenario
10.Build updateEmployee REST API
11.Unit test updateEmployee REST API - Positive Scenario
12.Unit test updateEmployee REST API - Negative Scenario
13.Build deleteEmployee REST API
14.Unit test deleteEmployee REST API
g

Hamcrest Librar
Hamcrest is the well-known framework used for unit testing in the Java ecosystem.
It's bundled in JUnit and simply put, it uses existing predicates – called matcher
classes – for making assertions.

Hamcrest is commonly used with JUnit and other testing frameworks for making
assertions. Speci cally, instead of using JUnit’s numerous assert methods, we only
use the API's single assertThat statement with appropriate matchers.

Hamcrest is() method: If we want to verify that the expected value (or object) is
equal to the actual value (or object), we have to create our Hamcrest matcher by
invoking the is() method of the Matchers class.
Syntax:
assertThat(ACTUAL, is(EXPECTED))

fi
y

JsonPath Librar
A Java DSL for reading JSON documents

JsonPath expressions always refer to a JSON structure in the same way as XPath
expression are used in combination with an XML document. The "root member
object" in JsonPath is always referred to as $ regardless if it is an object or array.

JSON JsonPath Expressions

$ - root member of a JSON structure whether it is an object


" rstName": "Ramesh" or array
“lastName": “Fadatare" $. rstName = “Ramesh
“email": "[email protected] $.lastName = “Fadatare
} $.email = “[email protected]
{

fi
fi
.

"

JUnit tests in BDD Style


Example

Syntax

@Test
public void given_when_then() {
// given - precondition or setup

// when - action or the behaviour we’re testing

// then - verify the output


}

@WebMvcTest Annotatio
SpringBoot provides @WebMvcTest annotation to test Spring MVC Controllers.
Also, @WebMvcTest based tests runs faster as it will load only the speci ed controller
and its dependencies only without loading the entire application

Spring Boot instantiates only the web layer rather than the whole application context. In
an application with multiple controllers, you can even ask for only one to be
instantiated by using, for example, @WebMvcTest(HomeController.class).

@WebMvcTest

HomeController
Controller Service Repository
EmployeeController DB
UserController
Layer Layer Layer

fi
@WebMvcTest vs @SpringBootTes
Spring Boot provides @WebMvcTest annotation to test Spring MVC controllers. This
annotation creates an application context that contains all the beans necessary for testing
a Spring web controller.

Spring Boot provides @SpringBootTest annotation for Integration testing. This


annotation creates an application context and loads full application context.

Unit testing - @WebMvcTest annotation

Integration testing - @SpringBootTest

HomeController
Controller Service Repository
EmployeeController DB
UserController
Layer Layer Layer

You might also like