Skip to main content

How to: test programmatic authorization checks in a unit test

Unit tests are meant to test classes in isolation. Consequently, these tests also take place outside a Spring application context. Therefore, neither the autoconfiguration of the jeap-spring-boot-security-starter nor any declarative authorization checks have any influence on the test. However, if a method under test performs programmatic authorization checks, a JeapAuthenticationToken must be provided for it.

Steps​

  1. Add the test dependency jeap-spring-boot-security-starter-test to the project (see Integration).
  2. Mock the JeapAuthenticationToken for the programmatic authorization check under test, for example with the AuthorizationMock classes — ServletSimpleAuthorizationMock or ServletSemanticAuthorizationMock — storing a token created with the JeapAuthenticationTestTokenBuilder. Alternatively, plain Mockito mocks can be used for this as well.

This example shows how the JeapAuthenticationToken can be mocked for the test of a method with a programmatic authorization check.