Skip to content

Commit be3177d

Browse files
committed
Remove mockito from powertools-common.
1 parent ac48107 commit be3177d

File tree

8 files changed

+301
-70
lines changed

8 files changed

+301
-70
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
<aws.sdk.v1.version>1.12.781</aws.sdk.v1.version>
114114
<versions-maven-plugin.version>2.18.0</versions-maven-plugin.version>
115115
<elastic.version>1.7.0</elastic.version>
116-
<mockito.version>5.16.0</mockito.version>
117-
<mockito-junit-jupiter.version>5.16.0</mockito-junit-jupiter.version>
116+
<mockito.version>5.19.0</mockito.version>
117+
<mockito-junit-jupiter.version>5.19.0</mockito-junit-jupiter.version>
118118
<junit-pioneer.version>2.3.0</junit-pioneer.version>
119119
<crac.version>1.5.0</crac.version>
120120

@@ -321,7 +321,7 @@
321321
<dependency>
322322
<groupId>org.junit-pioneer</groupId>
323323
<artifactId>junit-pioneer</artifactId>
324-
<version>2.0.0</version>
324+
<version>${junit-pioneer.version}</version>
325325
<scope>test</scope>
326326
</dependency>
327327
<dependency>

powertools-common/pom.xml

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
-->
1515

1616
<project xmlns="http://maven.apache.org/POM/4.0.0"
17-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1919
<modelVersion>4.0.0</modelVersion>
2020

2121
<artifactId>powertools-common</artifactId>
@@ -76,11 +76,6 @@
7676
<artifactId>assertj-core</artifactId>
7777
<scope>test</scope>
7878
</dependency>
79-
<dependency>
80-
<groupId>org.mockito</groupId>
81-
<artifactId>mockito-core</artifactId>
82-
<scope>test</scope>
83-
</dependency>
8479
<dependency>
8580
<groupId>org.slf4j</groupId>
8681
<artifactId>slf4j-simple</artifactId>
@@ -90,21 +85,15 @@
9085
<profiles>
9186
<profile>
9287
<id>generate-graalvm-files</id>
93-
<dependencies>
94-
<dependency>
95-
<groupId>org.mockito</groupId>
96-
<artifactId>mockito-subclass</artifactId>
97-
<scope>test</scope>
98-
</dependency>
99-
</dependencies>
10088
<build>
10189
<plugins>
10290
<plugin>
10391
<groupId>org.apache.maven.plugins</groupId>
10492
<artifactId>maven-surefire-plugin</artifactId>
10593
<version>3.5.3</version>
10694
<configuration>
107-
<argLine>-Dmockito.mock.maker=subclass -Dorg.graalvm.nativeimage.imagecode=agent -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-common,experimental-class-define-support
95+
<argLine>-Dorg.graalvm.nativeimage.imagecode=agent
96+
-agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-common,experimental-class-define-support
10897
--add-opens java.base/java.util=ALL-UNNAMED
10998
--add-opens java.base/java.lang=ALL-UNNAMED
11099
</argLine>
@@ -115,13 +104,6 @@
115104
</profile>
116105
<profile>
117106
<id>graalvm-native</id>
118-
<dependencies>
119-
<dependency>
120-
<groupId>org.mockito</groupId>
121-
<artifactId>mockito-subclass</artifactId>
122-
<scope>test</scope>
123-
</dependency>
124-
</dependencies>
125107
<build>
126108
<plugins>
127109
<plugin>
@@ -145,28 +127,11 @@
145127
<buildArg>--add-opens java.base/java.lang=ALL-UNNAMED</buildArg>
146128
<buildArg>--no-fallback</buildArg>
147129
<buildArg>-Dorg.graalvm.nativeimage.imagecode=agent</buildArg>
148-
<buildArg>-H:ClassInitialization=net.bytebuddy.ClassFileVersion:rerun</buildArg>
149-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.dispatcher.JavaDispatcher:rerun</buildArg>
150-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.Invoker$Dispatcher:rerun</buildArg>
151-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.GraalImageCode:rerun</buildArg>
152130
<buildArg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger</buildArg>
153131
<buildArg>--initialize-at-build-time=org.slf4j.LoggerFactory</buildArg>
154132
<buildArg>--initialize-at-build-time=org.junit.Ignore</buildArg>
155133
<buildArg>--initialize-at-build-time=java.lang.annotation.Annotation</buildArg>
156134
<buildArg>--initialize-at-build-time=org.junit.runners.model.FrameworkField</buildArg>
157-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$AbstractBase</buildArg>
158-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic</buildArg>
159-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic$1</buildArg>
160-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic$2</buildArg>
161-
<buildArg>--initialize-at-build-time=net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader</buildArg>
162-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.method.MethodDescription$InDefinedShape$AbstractBase$ForLoadedExecutable</buildArg>
163-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$AbstractBase</buildArg>
164-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$ForLoadedType</buildArg>
165-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.method.MethodDescription$ForLoadedMethod</buildArg>
166-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Super$Instantiation$2</buildArg>
167-
<buildArg>
168-
--trace-class-initialization=net.bytebuddy.description.type.TypeDescription$ForLoadedType,net.bytebuddy.description.method.MethodDescription$ForLoadedMethod,net.bytebuddy.description.method.MethodDescription$InDefinedShape$AbstractBase$ForLoadedExecutable
169-
</buildArg>
170135
<buildArg>--verbose</buildArg>
171136
<buildArg>--native-image-info</buildArg>
172137
<buildArg>-H:+UnlockExperimentalVMOptions</buildArg>

powertools-common/src/test/java/software/amazon/lambda/powertools/common/internal/LambdaHandlerProcessorTest.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static org.junit.jupiter.api.Assertions.assertNotNull;
19-
import static org.mockito.Mockito.mock;
20-
import static org.mockito.Mockito.when;
2119

22-
import java.io.InputStream;
23-
import java.io.OutputStream;
2420
import java.util.Optional;
2521

2622
import org.aspectj.lang.ProceedingJoinPoint;
27-
import org.aspectj.lang.Signature;
2823
import org.junit.jupiter.api.Test;
2924
import org.junitpioneer.jupiter.ClearEnvironmentVariable;
3025
import org.junitpioneer.jupiter.SetEnvironmentVariable;
@@ -33,22 +28,26 @@
3328
import com.amazonaws.services.lambda.runtime.RequestHandler;
3429
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
3530

36-
class LambdaHandlerProcessorTest {
31+
import software.amazon.lambda.powertools.common.stubs.TestLambdaContext;
32+
import software.amazon.lambda.powertools.common.stubs.TestInputStream;
33+
import software.amazon.lambda.powertools.common.stubs.TestOutputStream;
34+
import software.amazon.lambda.powertools.common.stubs.TestProceedingJoinPoint;
35+
import software.amazon.lambda.powertools.common.stubs.TestSignature;
3736

38-
private Signature signature = mock(Signature.class);
39-
private ProceedingJoinPoint pjpMock = mock(ProceedingJoinPoint.class);
37+
class LambdaHandlerProcessorTest {
4038

4139
@Test
4240
void isHandlerMethod_shouldRecognizeRequestHandler() {
43-
Object[] args = {new Object(), mock(Context.class)};
41+
Context context = new TestLambdaContext();
42+
Object[] args = { new Object(), context };
4443
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestHandler.class, args);
4544

4645
assertThat(LambdaHandlerProcessor.isHandlerMethod(pjpMock)).isTrue();
4746
}
4847

4948
@Test
5049
void isHandlerMethod_shouldRecognizeRequestStreamHandler() {
51-
Object[] args = {mock(InputStream.class), mock(OutputStream.class), mock(Context.class)};
50+
Object[] args = { new TestInputStream(), new TestOutputStream(), new TestLambdaContext() };
5251
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
5352

5453
assertThat(LambdaHandlerProcessor.isHandlerMethod(pjpMock)).isTrue();
@@ -65,23 +64,23 @@ void isHandlerMethod_shouldReturnFalse() {
6564

6665
@Test
6766
void placedOnRequestHandler_shouldRecognizeRequestHandler() {
68-
Object[] args = {new Object(), mock(Context.class)};
67+
Object[] args = { new Object(), new TestLambdaContext() };
6968
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestHandler.class, args);
7069

7170
assertThat(LambdaHandlerProcessor.placedOnRequestHandler(pjpMock)).isTrue();
7271
}
7372

7473
@Test
7574
void placedOnStreamHandler_shouldRecognizeRequestStreamHandler() {
76-
Object[] args = {mock(InputStream.class), mock(OutputStream.class), mock(Context.class)};
75+
Object[] args = { new TestInputStream(), new TestOutputStream(), new TestLambdaContext() };
7776
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
7877

7978
assertThat(LambdaHandlerProcessor.placedOnStreamHandler(pjpMock)).isTrue();
8079
}
8180

8281
@Test
8382
void placedOnRequestHandler_shouldInvalidateOnWrongNoOfArgs() {
84-
Object[] args = {new Object()};
83+
Object[] args = { new Object() };
8584
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestHandler.class, args);
8685

8786
boolean isPlacedOnRequestHandler = LambdaHandlerProcessor.placedOnRequestHandler(pjpMock);
@@ -91,7 +90,7 @@ void placedOnRequestHandler_shouldInvalidateOnWrongNoOfArgs() {
9190

9291
@Test
9392
void placedOnRequestHandler_shouldInvalidateOnWrongTypeOfArgs() {
94-
Object[] args = {new Object(), new Object()};
93+
Object[] args = { new Object(), new Object() };
9594
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestHandler.class, args);
9695

9796
boolean isPlacedOnRequestHandler = LambdaHandlerProcessor.placedOnRequestHandler(pjpMock);
@@ -101,7 +100,7 @@ void placedOnRequestHandler_shouldInvalidateOnWrongTypeOfArgs() {
101100

102101
@Test
103102
void placedOnStreamHandler_shouldInvalidateOnWrongNoOfArgs() {
104-
Object[] args = {new Object()};
103+
Object[] args = { new Object() };
105104
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
106105

107106
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
@@ -111,7 +110,7 @@ void placedOnStreamHandler_shouldInvalidateOnWrongNoOfArgs() {
111110

112111
@Test
113112
void placedOnStreamHandler_shouldInvalidateOnWrongTypeOfArgs() {
114-
Object[] args = {new Object(), new Object(), new Object()};
113+
Object[] args = { new Object(), new Object(), new Object() };
115114
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
116115

117116
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
@@ -121,7 +120,7 @@ void placedOnStreamHandler_shouldInvalidateOnWrongTypeOfArgs() {
121120

122121
@Test
123122
void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidOutputStreamArg() {
124-
Object[] args = {mock(InputStream.class), new Object(), mock(Context.class)};
123+
Object[] args = { new TestInputStream(), new Object(), new TestLambdaContext() };
125124
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
126125

127126
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
@@ -131,7 +130,7 @@ void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidOutputStreamArg()
131130

132131
@Test
133132
void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidContextArg() {
134-
Object[] args = {mock(InputStream.class), mock(OutputStream.class), new Object()};
133+
Object[] args = { new TestInputStream(), new TestOutputStream(), new Object() };
135134
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
136135

137136
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
@@ -144,9 +143,9 @@ void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidContextArg() {
144143
void getXrayTraceId_present() {
145144
String traceID = "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"";
146145

147-
Optional xRayTraceId = LambdaHandlerProcessor.getXrayTraceId();
146+
Optional<String> xRayTraceId = LambdaHandlerProcessor.getXrayTraceId();
148147

149-
assertThat(xRayTraceId.isPresent()).isTrue();
148+
assertThat(xRayTraceId).isPresent();
150149
assertThat(traceID.split(";")[0].replace(LambdaConstants.ROOT_EQUALS, "")).isEqualTo(xRayTraceId.get());
151150
}
152151

@@ -161,7 +160,7 @@ void getXrayTraceId_notPresent() {
161160

162161
@Test
163162
void extractContext_fromRequestHandler() {
164-
Object[] args = {new Object(), mock(Context.class)};
163+
Object[] args = { new Object(), new TestLambdaContext() };
165164
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestHandler.class, args);
166165

167166
Context context = LambdaHandlerProcessor.extractContext(pjpMock);
@@ -171,7 +170,7 @@ void extractContext_fromRequestHandler() {
171170

172171
@Test
173172
void extractContext_fromStreamRequestHandler() {
174-
Object[] args = {mock(InputStream.class), mock(OutputStream.class), mock(Context.class)};
173+
Object[] args = { new TestInputStream(), new TestOutputStream(), new TestLambdaContext() };
175174
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
176175

177176
Context context = LambdaHandlerProcessor.extractContext(pjpMock);
@@ -181,7 +180,7 @@ void extractContext_fromStreamRequestHandler() {
181180

182181
@Test
183182
void extractContext_notKnownHandler() {
184-
Object[] args = {new Object()};
183+
Object[] args = { new Object() };
185184
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(Object.class, args);
186185

187186
Context context = LambdaHandlerProcessor.extractContext(pjpMock);
@@ -230,10 +229,8 @@ void serviceName_Undefined() {
230229
assertThat(LambdaHandlerProcessor.serviceName()).isEqualTo(LambdaConstants.SERVICE_UNDEFINED);
231230
}
232231

233-
private ProceedingJoinPoint mockRequestHandlerPjp(Class handlerClass, Object[] handlerArgs) {
234-
when(signature.getDeclaringType()).thenReturn(handlerClass);
235-
when(pjpMock.getArgs()).thenReturn(handlerArgs);
236-
when(pjpMock.getSignature()).thenReturn(signature);
237-
return pjpMock;
232+
private ProceedingJoinPoint mockRequestHandlerPjp(Class<?> handlerClass, Object[] handlerArgs) {
233+
TestSignature signature = new TestSignature(handlerClass);
234+
return new TestProceedingJoinPoint(signature, handlerArgs);
238235
}
239236
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
15+
package software.amazon.lambda.powertools.common.stubs;
16+
17+
import java.io.InputStream;
18+
19+
public class TestInputStream extends InputStream {
20+
@Override
21+
public int read() {
22+
return -1;
23+
}
24+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
15+
package software.amazon.lambda.powertools.common.stubs;
16+
17+
import com.amazonaws.services.lambda.runtime.ClientContext;
18+
import com.amazonaws.services.lambda.runtime.CognitoIdentity;
19+
import com.amazonaws.services.lambda.runtime.Context;
20+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
21+
22+
public class TestLambdaContext implements Context {
23+
@Override
24+
public String getAwsRequestId() {
25+
return "test-request-id";
26+
}
27+
28+
@Override
29+
public String getLogGroupName() {
30+
return "test-log-group";
31+
}
32+
33+
@Override
34+
public String getLogStreamName() {
35+
return "test-log-stream";
36+
}
37+
38+
@Override
39+
public String getFunctionName() {
40+
return "test-function";
41+
}
42+
43+
@Override
44+
public String getFunctionVersion() {
45+
return "1";
46+
}
47+
48+
@Override
49+
public String getInvokedFunctionArn() {
50+
return "arn:aws:lambda:us-east-1:123456789012:function:test";
51+
}
52+
53+
@Override
54+
public CognitoIdentity getIdentity() {
55+
return null;
56+
}
57+
58+
@Override
59+
public ClientContext getClientContext() {
60+
return null;
61+
}
62+
63+
@Override
64+
public int getRemainingTimeInMillis() {
65+
return 30000;
66+
}
67+
68+
@Override
69+
public int getMemoryLimitInMB() {
70+
return 128;
71+
}
72+
73+
@Override
74+
public LambdaLogger getLogger() {
75+
return null;
76+
}
77+
}

0 commit comments

Comments
 (0)