As we’ve already covered in the Subject reference, we know that a Subject is security-specific view of the 'currently executing' user, and that Subject instances are always bound to a thread to ensure we know who is executing logic at any time during the thread’s execution.
This means three basic things must always occur in order to support being able to access the currently executing Subject:
Subject instance must be created
Subject instance must be bound to the currently executing thread.
After the thread is finished executing (or if the thread’s execution results in a
Subject must be unbound to ensure that the thread remains 'clean' in any thread-pooled environment.
Shiro has architectural components that perform this bind/unbind logic automatically for a running application. For example, in a web application, the root Shiro Filter performs this logic when filtering a request. But as test environments and frameworks differ, we need to perform this bind/unbind logic ourselves for our chosen test framework.