bug-fix: Use Junit5 in the serverless module tests (#1794)

* #1667: Fixing the serverless tests to use Junit5 and also modifying other classes to remove the deprecated initMock() method

* #1667: Fixing the sonar code smells

Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
This commit is contained in:
Nagaraj Tantri 2021-09-29 00:09:19 +05:30 committed by GitHub
parent be59e50205
commit be25c0b433
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 39 additions and 47 deletions

View File

@ -48,7 +48,7 @@ class AggregatorTest {
@BeforeEach @BeforeEach
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
/** /**

View File

@ -48,7 +48,7 @@ class ApiGatewayTest {
@BeforeEach @BeforeEach
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
/** /**

View File

@ -68,7 +68,7 @@ class ThreadAsyncExecutorTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
/** /**

View File

@ -46,7 +46,7 @@ class DataBusTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
@Test @Test

View File

@ -62,7 +62,7 @@
<slf4j.version>1.7.30</slf4j.version> <slf4j.version>1.7.30</slf4j.version>
<logback.version>1.2.3</logback.version> <logback.version>1.2.3</logback.version>
<aws-lambda-core.version>1.1.0</aws-lambda-core.version> <aws-lambda-core.version>1.1.0</aws-lambda-core.version>
<aws-java-sdk-dynamodb.version>1.11.289</aws-java-sdk-dynamodb.version> <aws-java-sdk-dynamodb.version>1.12.13</aws-java-sdk-dynamodb.version>
<aws-lambda-java-events.version>2.0.1</aws-lambda-java-events.version> <aws-lambda-java-events.version>2.0.1</aws-lambda-java-events.version>
<jackson.version>2.12.3</jackson.version> <jackson.version>2.12.3</jackson.version>
<jaxb-api.version>2.3.1</jaxb-api.version> <jaxb-api.version>2.3.1</jaxb-api.version>
@ -72,7 +72,7 @@
<urm.version>2.0.0</urm.version> <urm.version>2.0.0</urm.version>
<mockito-junit-jupiter.version>3.5.0</mockito-junit-jupiter.version> <mockito-junit-jupiter.version>3.5.0</mockito-junit-jupiter.version>
<lombok.version>1.18.14</lombok.version> <lombok.version>1.18.14</lombok.version>
<byte-buddy.version>1.10.21</byte-buddy.version> <byte-buddy.version>1.11.5</byte-buddy.version>
<javassist.version>3.27.0-GA</javassist.version> <javassist.version>3.27.0-GA</javassist.version>
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version> <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version> <maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version>

View File

@ -44,16 +44,6 @@
<groupId>com.amazonaws</groupId> <groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId> <artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws-java-sdk-dynamodb.version}</version> <version>${aws-java-sdk-dynamodb.version}</version>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</exclusion>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-kms</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.amazonaws</groupId> <groupId>com.amazonaws</groupId>
@ -80,15 +70,15 @@
<artifactId>junit-jupiter-engine</artifactId> <artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.hamcrest</groupId>
<artifactId>mockito-core</artifactId> <artifactId>hamcrest-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -23,6 +23,9 @@
package com.iluwatar.serverless.baas.api; package com.iluwatar.serverless.baas.api;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -32,31 +35,29 @@ import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.iluwatar.serverless.baas.model.Person; import com.iluwatar.serverless.baas.model.Person;
import java.util.Map; import java.util.Map;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/** /**
* Unit tests for FindPersonApiHandler Created by dheeraj.mummar on 3/5/18. * Unit tests for FindPersonApiHandler Created by dheeraj.mummar on 3/5/18.
*/ */
@RunWith(MockitoJUnitRunner.class) class FindPersonApiHandlerTest {
public class FindPersonApiHandlerTest {
private FindPersonApiHandler findPersonApiHandler; private FindPersonApiHandler findPersonApiHandler;
@Mock @Mock
private DynamoDBMapper dynamoDbMapper; private DynamoDBMapper dynamoDbMapper;
@Before @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.openMocks(this);
this.findPersonApiHandler = new FindPersonApiHandler(); this.findPersonApiHandler = new FindPersonApiHandler();
this.findPersonApiHandler.setDynamoDbMapper(dynamoDbMapper); this.findPersonApiHandler.setDynamoDbMapper(dynamoDbMapper);
} }
@Test @Test
public void handleRequest() { void handleRequest() {
findPersonApiHandler.handleRequest(apiGatewayProxyRequestEvent(), mock(Context.class)); findPersonApiHandler.handleRequest(apiGatewayProxyRequestEvent(), mock(Context.class));
verify(dynamoDbMapper, times(1)).load(Person.class, "37e7a1fe-3544-473d-b764-18128f02d72d"); verify(dynamoDbMapper, times(1)).load(Person.class, "37e7a1fe-3544-473d-b764-18128f02d72d");
} }

View File

@ -34,18 +34,18 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.iluwatar.serverless.baas.model.Address; import com.iluwatar.serverless.baas.model.Address;
import com.iluwatar.serverless.baas.model.Person; import com.iluwatar.serverless.baas.model.Person;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.MockitoAnnotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* Unit tests for SavePersonApiHandler Created by dheeraj.mummar on 3/4/18. * Unit tests for SavePersonApiHandler Created by dheeraj.mummar on 3/4/18.
*/ */
@RunWith(MockitoJUnitRunner.class) class SavePersonApiHandlerTest {
public class SavePersonApiHandlerTest {
private SavePersonApiHandler savePersonApiHandler; private SavePersonApiHandler savePersonApiHandler;
@ -54,31 +54,32 @@ public class SavePersonApiHandlerTest {
private final ObjectMapper objectMapper = new ObjectMapper(); private final ObjectMapper objectMapper = new ObjectMapper();
@Before @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.openMocks(this);
this.savePersonApiHandler = new SavePersonApiHandler(); this.savePersonApiHandler = new SavePersonApiHandler();
this.savePersonApiHandler.setDynamoDbMapper(dynamoDbMapper); this.savePersonApiHandler.setDynamoDbMapper(dynamoDbMapper);
} }
@Test @Test
public void handleRequestSavePersonSuccessful() throws JsonProcessingException { void handleRequestSavePersonSuccessful() throws JsonProcessingException {
var person = newPerson(); var person = newPerson();
var body = objectMapper.writeValueAsString(person); var body = objectMapper.writeValueAsString(person);
var request = apiGatewayProxyRequestEvent(body); var request = apiGatewayProxyRequestEvent(body);
var ctx = mock(Context.class); var ctx = mock(Context.class);
var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx); var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx);
verify(dynamoDbMapper, times(1)).save(person); verify(dynamoDbMapper, times(1)).save(person);
Assert.assertNotNull(apiGatewayProxyResponseEvent); assertNotNull(apiGatewayProxyResponseEvent);
Assert.assertEquals(Integer.valueOf(201), apiGatewayProxyResponseEvent.getStatusCode()); assertEquals(Integer.valueOf(201), apiGatewayProxyResponseEvent.getStatusCode());
} }
@Test @Test
public void handleRequestSavePersonException() { void handleRequestSavePersonException() {
var request = apiGatewayProxyRequestEvent("invalid sample request"); var request = apiGatewayProxyRequestEvent("invalid sample request");
var ctx = mock(Context.class); var ctx = mock(Context.class);
var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx); var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx);
Assert.assertNotNull(apiGatewayProxyResponseEvent); assertNotNull(apiGatewayProxyResponseEvent);
Assert.assertEquals(Integer.valueOf(400), apiGatewayProxyResponseEvent.getStatusCode()); assertEquals(Integer.valueOf(400), apiGatewayProxyResponseEvent.getStatusCode());
} }
private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent(String body) { private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent(String body) {