Simplify unit tests with JUnit 5 and Apache Shiro.
@ExtendWith(ShiroExtension.class)
@SubjectAware(
value = "trillian",
roles = "user",
permissions = "one:*"
)
class ShiroExtensionTest {
@Test
void shouldHavePreparedSubject() {
Subject subject = SecurityUtils.getSubject();
assertThat(subject.getPrincipal()).isEqualTo("trillian");
assertThat(subject.hasRole("user")).isTrue();
assertThat(subject.isPermitted("one:dot:one")).isTrue();
}
}
The SubjectAware
annotation can be placed on classes, nested classes or on methods.
Roles and permissions are merged. If the value
is of SubjectAware
is empty, no subject is bound.
For more samples have a look at the ShiroExtensionTest.
Get the latest stable version from
testImplementation 'com.github.sdorra:junit-shiro-extension:x.y.z'
<dependency>
<groupId>com.github.sdorra</groupId>
<artifactId>junit-shiro-extension</artifactId>
<version>x.y.z</version>
<scope>test</scope>
</dependency>
This project is licensed under the MIT License - see the LICENSE file for details