Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 1cfe166

Browse files
author
Stefan Kock
committed
SORMAS-Foundation#4133 Avoid mocking of InvocationContext
1 parent 9a1822e commit 1cfe166

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

sormas-backend/src/main/java/de/symeda/sormas/backend/util/PerformanceLoggingInterceptor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,19 @@ public Object logInvokeDuration(InvocationContext context) throws Exception {
5858
*/
5959
static String getInvokedMethod(InvocationContext context) {
6060

61-
String targetName = context.getTarget().toString();
61+
return getInvokedMethod(context.getTarget().toString(), context.getMethod().getName());
62+
}
63+
64+
/**
65+
* @return Better readable method name (with EJB class) that was invoked.
66+
*/
67+
static String getInvokedMethod(String targetName, String methodName) {
68+
6269
int classBegin = targetName.lastIndexOf(".") + 1;
6370
int innerClassBegin = targetName.indexOf("$");
6471
int instanceBegin = targetName.indexOf("@");
6572

6673
String ejbName = targetName.substring(classBegin, innerClassBegin > 0 ? innerClassBegin : instanceBegin);
67-
return String.format("%s.%s", ejbName, context.getMethod().getName());
74+
return String.format("%s.%s", ejbName, methodName);
6875
}
6976
}

sormas-backend/src/test/java/de/symeda/sormas/backend/util/PerformanceLoggingInterceptorTest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.equalTo;
5-
import static org.mockito.Mockito.mock;
6-
import static org.mockito.Mockito.when;
7-
8-
import java.lang.reflect.Method;
9-
10-
import javax.interceptor.InvocationContext;
115

126
import org.junit.Test;
137

@@ -17,18 +11,19 @@
1711
*/
1812
public class PerformanceLoggingInterceptorTest {
1913

14+
/**
15+
* Only tests the String inspection but avoids mocking due to massive performance impacts on other unit tests in the same test run.
16+
*/
2017
@Test
2118
public void testGetInvokedMethod() {
2219

23-
InvocationContext context = mock(InvocationContext.class);
24-
Method method = mock(Method.class);
25-
when(context.getMethod()).thenReturn(method);
26-
27-
when(context.getTarget()).thenReturn("de.symeda.sormas.backend.region.CommunityService@15301bba");
28-
when(context.getMethod().getName()).thenReturn("count");
29-
assertThat(PerformanceLoggingInterceptor.getInvokedMethod(context), equalTo("CommunityService.count"));
20+
assertThat(
21+
PerformanceLoggingInterceptor.getInvokedMethod("de.symeda.sormas.backend.region.CommunityService@15301bba", "count"),
22+
equalTo("CommunityService.count"));
3023

31-
when(context.getTarget()).thenReturn("de.symeda.sormas.backend.facility.FacilityFacadeEjb$FacilityFacadeEjbLocal@630e5556");
32-
assertThat(PerformanceLoggingInterceptor.getInvokedMethod(context), equalTo("FacilityFacadeEjb.count"));
24+
assertThat(
25+
PerformanceLoggingInterceptor
26+
.getInvokedMethod("de.symeda.sormas.backend.facility.FacilityFacadeEjb$FacilityFacadeEjbLocal@630e5556", "count"),
27+
equalTo("FacilityFacadeEjb.count"));
3328
}
3429
}

0 commit comments

Comments
 (0)