java/com.sap.sailing.server.test/src/com/sap/sailing/server/test/MasterDataImportTest.java
... ...
@@ -35,6 +35,7 @@ import javax.ws.rs.core.StreamingOutput;
35 35
import org.apache.shiro.SecurityUtils;
36 36
import org.apache.shiro.mgt.SecurityManager;
37 37
import org.apache.shiro.subject.Subject;
38
+import org.apache.shiro.util.ThreadContext;
38 39
import org.junit.jupiter.api.AfterEach;
39 40
import org.junit.jupiter.api.Assertions;
40 41
import org.junit.jupiter.api.BeforeEach;
... ...
@@ -194,6 +195,8 @@ public class MasterDataImportTest {
194 195
195 196
@AfterEach
196 197
public void tearDown() {
198
+ ThreadContext.unbindSecurityManager();
199
+ ThreadContext.unbindSubject();
197 200
deleteAllDataFromDatabase();
198 201
}
199 202
... ...
@@ -205,8 +208,15 @@ public class MasterDataImportTest {
205 208
securityService = Mockito.mock(SecurityService.class);
206 209
SecurityManager securityManager = Mockito.mock(org.apache.shiro.mgt.SecurityManager.class);
207 210
Subject fakeSubject = Mockito.mock(Subject.class);
208
- SecurityUtils.setSecurityManager(securityManager);
211
+ // Stub the mock BEFORE installing it as the global SecurityManager to avoid a race
212
+ // condition where a background thread (from RacingEventServiceImpl's static
213
+ // ScheduledExecutorService) calls SecurityUtils.getSubject(), which triggers
214
+ // createSubject() on the mock while Mockito is still in the middle of setting up
215
+ // the doReturn().when() stubbing. The background thread consumes/corrupts
216
+ // Mockito's doAnswer-style answers on the mock's InvocationContainer, causing
217
+ // an AssertionError in InvocationContainerImpl.setMethodForStubbing.
209 218
Mockito.doReturn(fakeSubject).when(securityManager).createSubject(Mockito.any());
219
+ SecurityUtils.setSecurityManager(securityManager);
210 220
Mockito.doReturn(defaultTenant).when(securityService).getServerGroup();
211 221
Mockito.doReturn(currentUser).when(securityService).getCurrentUser();
212 222
Mockito.doReturn(true).when(securityService).hasCurrentUserReadPermission(Mockito.any());
java/com.sap.sailing.server.test/src/com/sap/sailing/server/test/SearchServiceTest.java
... ...
@@ -20,6 +20,8 @@ import java.util.UUID;
20 20
import org.apache.shiro.SecurityUtils;
21 21
import org.apache.shiro.mgt.SecurityManager;
22 22
import org.apache.shiro.subject.Subject;
23
+import org.apache.shiro.util.ThreadContext;
24
+import org.junit.jupiter.api.AfterEach;
23 25
import org.junit.jupiter.api.BeforeEach;
24 26
import org.junit.jupiter.api.Test;
25 27
import org.mockito.Mockito;
... ...
@@ -114,6 +116,12 @@ public class SearchServiceTest {
114 116
private DynamicTrackedRace aalOrcTrackedR2;
115 117
private SecurityService securityService;
116 118
119
+ @AfterEach
120
+ public void tearDown() {
121
+ ThreadContext.unbindSecurityManager();
122
+ ThreadContext.unbindSubject();
123
+ }
124
+
117 125
@BeforeEach
118 126
public void setUp() {
119 127
UserGroupImpl defaultTenant = new UserGroupImpl(new UUID(0, 1), "defaultTenant");
... ...
@@ -121,8 +129,15 @@ public class SearchServiceTest {
121 129
securityService = Mockito.mock(SecurityService.class);
122 130
SecurityManager securityManager = Mockito.mock(org.apache.shiro.mgt.SecurityManager.class);
123 131
Subject fakeSubject = Mockito.mock(Subject.class);
124
- SecurityUtils.setSecurityManager(securityManager);
132
+ // Stub the mock BEFORE installing it as the global SecurityManager to avoid a race
133
+ // condition where a background thread (from RacingEventServiceImpl's static
134
+ // ScheduledExecutorService) calls SecurityUtils.getSubject(), which triggers
135
+ // createSubject() on the mock while Mockito is still in the middle of setting up
136
+ // the doReturn().when() stubbing. The background thread consumes/corrupts
137
+ // Mockito's doAnswer-style answers on the mock's InvocationContainer, causing
138
+ // an AssertionError in InvocationContainerImpl.setMethodForStubbing.
125 139
Mockito.doReturn(fakeSubject).when(securityManager).createSubject(Mockito.any());
140
+ SecurityUtils.setSecurityManager(securityManager);
126 141
Mockito.doReturn(defaultTenant).when(securityService).getServerGroup();
127 142
Mockito.doReturn(currentUser).when(securityService).getCurrentUser();
128 143
Mockito.doReturn(true).when(securityService).hasCurrentUserReadPermission(Mockito.any());