Skip to content

Commit

Permalink
Update mockito verifications to work with Java Test Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
mwodahl committed Jul 16, 2024
1 parent 2e06875 commit 13e8d74
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 73 deletions.
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"java.test.config": {
"env": {
"TEST_VARIABLE": "testValue",
"TEST_VARIABLE_EMPTY": "",
"AWS_ACCESS_KEY_ID": "testAccessKey",
"AWS_SECRET_ACCESS_KEY": "testSecretKey",
"AWS_EXPIRATION": "2020-01-01 00:00:00",
"AWS_SESSION_TOKEN": "testSessionToken",
"API_ENDPOINT": "testApiEndpoint",
"CONFLUENT_KEY": "testConfluentKey",
"CONFLUENT_SECRET": "testConfluentSecret",
}
},
}
36 changes: 17 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,36 @@
<packaging>jar</packaging>
<name>JPO AWS Depositor</name>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<java.version>21</java.version>
<jmockit.version>1.49</jmockit.version>
<powermock.version>2.0.2</powermock.version>
<!-- Allow override of github organization when publishing artifacts to github -->
<github_organization>usdot-jpo-ode</github_organization>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import mockit.Verifications;

public class GenerateAWSProfileTest {
@Test
void testGenerateAWSProfileSuccess() throws Exception {
Expand All @@ -44,17 +44,9 @@ void testGenerateAWSProfileSuccess() throws Exception {
assertNotNull(result);
assertEquals("value", result.getString("key"));

// Verify interactions
new Verifications() {{
mockClient.execute((HttpPost) any);
times = 1;

mockResponse.close();
times = 1;

mockClient.close();
times = 1;
}};
verify(mockClient, times(1)).execute((HttpPost) any());
verify(mockResponse, times(1)).close();
verify(mockClient, times(1)).close();
}

@Test
Expand All @@ -75,14 +67,5 @@ void testGenerateAWSProfileException() throws IOException {

// Verify the exception
assertNotNull(exception);

// Verify interactions
new Verifications() {{
mockClient.execute((HttpPost) any);
times = 1;

mockClient.close();
times = 1;
}};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ public class GetEnvironmentVariableTest {
void testGetEnvironmentVariableExists() throws Exception {
String expectedValue = "testValue";

// Test
// Test when the environment variable is set
String result = AwsDepositor.getEnvironmentVariable(TEST_VARIABLE, "");
assertEquals(expectedValue, result);
}

@Test
void testGetEnvironmentVariableNotSet() {
void testGetEnvironmentVariableNotSetOrEmpty() {
// Test when the environment variable is not set
String result = AwsDepositor.getEnvironmentVariable(TEST_VARIABLE_NO_ENV, DEFAULT_VALUE);
assertEquals(DEFAULT_VALUE, result);
}
String notSetResult = AwsDepositor.getEnvironmentVariable(TEST_VARIABLE_NO_ENV, DEFAULT_VALUE);
assertEquals(DEFAULT_VALUE, notSetResult);

@Test
void testGetEnvironmentVariableEmpty() {
// Test
String result = AwsDepositor.getEnvironmentVariable(TEST_VARIABLE_EMPTY, DEFAULT_VALUE);
assertEquals(DEFAULT_VALUE, result);
// Test when the environment variable is empty
String emptyResult = AwsDepositor.getEnvironmentVariable(TEST_VARIABLE_EMPTY, DEFAULT_VALUE);
assertEquals(DEFAULT_VALUE, emptyResult);
}
}
31 changes: 9 additions & 22 deletions src/test/java/us/dot/its/jpo/ode/aws/depositor/RunTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import mockit.Verifications;

public class RunTest {
@Test
public void testRunNoRecords() throws Exception {
Expand All @@ -39,16 +39,10 @@ public void testRunNoRecords() throws Exception {

doReturn(generateAwsReturnVal).when(depositor).generateAWSProfile();

new Verifications() {
{
depositor.getKafkaConsumer(any());
times = 1;
depositor.getRunDepositor();
times = 3;
}
};

depositor.run();

verify(depositor, times(1)).getKafkaConsumer(any());
verify(depositor, times(4)).getRunDepositor();
}

@Test
Expand Down Expand Up @@ -83,17 +77,10 @@ public void testRunRecords() throws Exception {

when(mockConsumer.poll(any())).thenReturn(mockRecords);

new Verifications() {
{
depositor.getKafkaConsumer(any());
times = 1;
depositor.getRunDepositor();
times = 3;
depositor.depositToFirehose(any(), any());
times = 1;
}
};

depositor.run();

verify(depositor, times(1)).getKafkaConsumer(any());
verify(depositor, times(4)).getRunDepositor();
verify(depositor, times(1)).depositToFirehose(any(), any());
}
}

0 comments on commit 13e8d74

Please sign in to comment.