Skip to content

Commit

Permalink
Merge pull request #264 from slachiewicz/mockito4
Browse files Browse the repository at this point in the history
Move Tests to Mockito4
  • Loading branch information
TVolden authored Oct 16, 2023
2 parents c6cf180 + 0d014bc commit 552436d
Show file tree
Hide file tree
Showing 45 changed files with 90 additions and 84 deletions.
4 changes: 2 additions & 2 deletions OCPP-J/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ dependencies {
compile project(':common')
compile 'com.google.code.gson:gson:2.8.0'
compile 'org.java-websocket:Java-WebSocket:1.5.3'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.13.2'
testCompile 'org.mockito:mockito-core:4.11.0'
testCompile 'org.hamcrest:hamcrest-core:1.3'
}

Expand Down
2 changes: 1 addition & 1 deletion OCPP-J/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import eu.chargetime.ocpp.wss.BaseWssFactoryBuilder;
Expand Down
4 changes: 2 additions & 2 deletions ocpp-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ dependencies {
compile group: 'javax.xml.soap', name: 'javax.xml.soap-api', version: '1.4.0'
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.1'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.13.2'
testCompile 'org.mockito:mockito-core:4.11.0'
testCompile 'org.hamcrest:hamcrest-all:1.3'
}

Expand Down
2 changes: 1 addition & 1 deletion ocpp-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ClientTest {
Expand All @@ -59,7 +59,7 @@ public class ClientTest {
@Before
public void setup() {
when(request.validate()).thenReturn(true);
doAnswer(invocation -> eventHandler = invocation.getArgumentAt(1, SessionEvents.class))
doAnswer(invocation -> eventHandler = invocation.getArgument(1, SessionEvents.class))
.when(session)
.open(any(), any());

Expand All @@ -77,7 +77,7 @@ public void connect_connects() {
client.connect(someUrl, events);

// Then
verify(session, times(1)).open(eq(someUrl), anyObject());
verify(session, times(1)).open(eq(someUrl), any());
}

@Test
Expand Down Expand Up @@ -112,14 +112,15 @@ public void send_aMessage_isCommunicated() throws Exception {
client.send(request);

// Then
verify(session, times(1)).sendRequest(anyString(), eq(request), anyString());
// TODO action and uuid should not be nullable
verify(session, times(1)).sendRequest(nullable(String.class), eq(request), nullable(String.class));
}

@Test
public void responseReceived_aMessageWasSend_PromiseIsCompleted() throws Exception {
// Given
String someUniqueId = "Some id";
when(session.storeRequest(any())).thenReturn(someUniqueId);
lenient().when(session.storeRequest(any())).thenReturn(someUniqueId);
when(promiseRepository.getPromise(any())).thenReturn(Optional.of(completableFuture));

// When
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.chargetime.ocpp.test;

import static org.mockito.Matchers.anyString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

import eu.chargetime.ocpp.*;
Expand All @@ -12,7 +12,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

/*
ChargeTime.eu - Java-OCA-OCPP
Expand Down Expand Up @@ -57,7 +57,7 @@ public void setup() throws Exception {

when(transactionRelatedRequest.transactionRelated()).thenReturn(true);
when(normalRequest.transactionRelated()).thenReturn(false);
doAnswer(invocation -> eventHandler = invocation.getArgumentAt(0, RadioEvents.class))
doAnswer(invocation -> eventHandler = invocation.getArgument(0, RadioEvents.class))
.when(receiver)
.accept(any());
setupCommunicator(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

/*
ChargeTime.eu - Java-OCA-OCPP
Expand Down Expand Up @@ -65,13 +65,13 @@ public void setup() {
UUID sessionId = UUID.randomUUID();
when(request.validate()).thenReturn(true);
when(session.getSessionId()).thenReturn(sessionId);
doAnswer(invocation -> listenerEvents = invocation.getArgumentAt(2, ListenerEvents.class))
doAnswer(invocation -> listenerEvents = invocation.getArgument(2, ListenerEvents.class))
.when(listener)
.open(anyString(), anyInt(), any());
doAnswer(invocation -> sessionEvents = invocation.getArgumentAt(0, SessionEvents.class))
doAnswer(invocation -> sessionEvents = invocation.getArgument(0, SessionEvents.class))
.when(session)
.accept(any());
doAnswer(invocation -> sessionIndex = invocation.getArgumentAt(0, UUID.class))
doAnswer(invocation -> sessionIndex = invocation.getArgument(0, UUID.class))
.when(serverEvents)
.newSession(any(), any());

Expand Down Expand Up @@ -114,7 +114,8 @@ public void send_aMessage_isCommunicated() throws Exception {
server.send(sessionIndex, request);

// Then
verify(session, times(1)).sendRequest(anyString(), eq(request), anyString());
// TODO action and uuid should not be nullable
verify(session, times(1)).sendRequest(nullable(String.class), eq(request), nullable(String.class));
}

@Test
Expand Down
28 changes: 17 additions & 11 deletions ocpp-common/src/test/java/eu/chargetime/ocpp/test/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

/*
ChargeTime.eu - Java-OCA-OCPP
Expand Down Expand Up @@ -63,7 +63,7 @@ public class SessionTest {
public void setup() throws Exception {
when(featureRepository.findFeature(any())).thenReturn(Optional.of(feature));
session = new Session(communicator, queue, fulfiller, featureRepository);
doAnswer(invocation -> eventHandler = invocation.getArgumentAt(1, CommunicatorEvents.class))
doAnswer(invocation -> eventHandler = invocation.getArgument(1, CommunicatorEvents.class))
.when(communicator)
.connect(any(), any());
session.open(null, sessionEvents);
Expand Down Expand Up @@ -103,7 +103,8 @@ public boolean validate() {
session.sendRequest(someAction, someRequest, null);

// Then
verify(communicator, times(1)).sendCall(anyString(), eq(someAction), eq(someRequest));
// TODO uniqueid should not be nullable
verify(communicator, times(1)).sendCall(nullable(String.class), eq(someAction), eq(someRequest));
}

@Test
Expand All @@ -115,7 +116,8 @@ public void sendRequest_someUniqueId_sendsUniqueIdToCommunicator() {
session.sendRequest(null, null, someUniqueId);

// Then
verify(communicator, times(1)).sendCall(eq(someUniqueId), anyString(), any());
// TODO uuid and request should not be nullable
verify(communicator, times(1)).sendCall(eq(someUniqueId), nullable(String.class), nullable(Request.class));
}

@Test
Expand Down Expand Up @@ -153,7 +155,7 @@ public void open_connectsViaCommunicator() {
public void onCall_unhandledCallback_callSendCallError() throws Exception {
// Given
String someId = "Some id";
doAnswer(invocation -> invocation.getArgumentAt(0, CompletableFuture.class).complete(null))
doAnswer(invocation -> invocation.getArgument(0, CompletableFuture.class).complete(null))
.when(fulfiller)
.fulfill(any(), any(), any());
when(communicator.unpackPayload(any(), any())).thenReturn(new TestRequest());
Expand All @@ -162,7 +164,8 @@ public void onCall_unhandledCallback_callSendCallError() throws Exception {
eventHandler.onCall(someId, null, null);

// then
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
// TODO action should not be nullable
verify(communicator, times(1)).sendCallError(eq(someId), nullable(String.class), anyString(), anyString());
}

@Test
Expand All @@ -178,7 +181,7 @@ public boolean validate() {

doAnswer(
invocation ->
invocation.getArgumentAt(0, CompletableFuture.class).complete(aConfirmation))
invocation.getArgument(0, CompletableFuture.class).complete(aConfirmation))
.when(fulfiller)
.fulfill(any(), any(), any());
when(communicator.unpackPayload(any(), any())).thenReturn(new TestRequest());
Expand All @@ -187,7 +190,8 @@ public boolean validate() {
eventHandler.onCall(someId, null, null);

// then
verify(communicator, times(1)).sendCallResult(anyString(), anyString(), eq(aConfirmation));
// TODO action should not be nullable
verify(communicator, times(1)).sendCallResult(anyString(), nullable(String.class), eq(aConfirmation));
}

@Test
Expand All @@ -197,7 +201,7 @@ public void onCall_callbackThrowsException_callSendCallResult() throws Exception
doAnswer(
invocation ->
invocation
.getArgumentAt(0, CompletableFuture.class)
.getArgument(0, CompletableFuture.class)
.completeExceptionally(new Exception()))
.when(fulfiller)
.fulfill(any(), any(), any());
Expand All @@ -207,7 +211,8 @@ public void onCall_callbackThrowsException_callSendCallResult() throws Exception
eventHandler.onCall(someId, null, null);

// then
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
// TODO uniqueid should not be nullable
verify(communicator, times(1)).sendCallError(eq(someId), nullable(String.class), anyString(), anyString());
}

@Test
Expand All @@ -229,6 +234,7 @@ public void onCall_unknownAction_callSendCallError() {
eventHandler.onCall(someId, null, null);

// Then
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
// TODO uniqueid should not be nullable
verify(communicator, times(1)).sendCallError(eq(someId), nullable(String.class), anyString(), anyString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ of this software and associated documentation files (the "Software"), to deal

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import eu.chargetime.ocpp.SessionEvents;
Expand All @@ -38,7 +38,7 @@ of this software and associated documentation files (the "Software"), to deal
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class SimpleRequestDispatcherTest {
Expand Down
6 changes: 3 additions & 3 deletions ocpp-v1_6/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ dependencies {
compile project(':OCPP-J')
compile 'org.java-websocket:Java-WebSocket:1.5.3'
compile group: 'javax.xml.soap', name: 'javax.xml.soap-api', version: '1.4.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'

testCompile 'junit:junit:4.13.2'
testCompile 'org.mockito:mockito-core:4.11.0'
testCompile 'org.hamcrest:hamcrest-core:1.3'
}

Expand Down
2 changes: 1 addition & 1 deletion ocpp-v1_6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

/*
ChargeTime.eu - Java-OCA-OCPP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -43,7 +43,7 @@ of this software and associated documentation files (the "Software"), to deal
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ClientFirmwareManagementProfileTest extends ProfileTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -45,7 +45,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ClientLocalAuthListProfileTest extends ProfileTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

/*
* ChargeTime.eu - Java-OCA-OCPP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -47,7 +47,7 @@ of this software and associated documentation files (the "Software"), to deal
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ClientReservationProfileTest extends ProfileTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

/*
* ChargeTime.eu - Java-OCA-OCPP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -17,7 +17,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

/*
ChargeTime.eu - Java-OCA-OCPP
Expand Down
Loading

0 comments on commit 552436d

Please sign in to comment.