How to do it...

Let us apply security model to some service methods and test them using Spring TestContext:

  1. Add the Spring Test module, JUnit 4, and Mockito Maven dependencies to the project's pom.xml file.
  2. Add a specialized Spring Security Test dependency to the pom.xml file:
        <dependency> 
            <groupId>org.springframework.security</groupId> 
            <artifactId>spring-security-test</artifactId> 
            <version>4.2.2.BUILD-SNAPSHOT</version> 
            <scope>test</scope> 
        </dependency> 
  1. Inside src/test/java, create a org.packt.mvc.secured.test package and drop inside it a test class that builds MockMvc from a secured WebApplicationContext method. Also, it tests controllers by providing some Spring Security details such as the username and password:
 import static  ...

Get Spring 5.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.