Skip to content

Commit

Permalink
Move to mockito 2.10
Browse files Browse the repository at this point in the history
Some changes are required like
 - anyString() --> nullable(String.class) for many tests as in fact we provide null values
 - add class for VerificationMode as there are more than one method now (can't use lambdas)
 - remove cast on ArgumentMatcher (now we can use lambdas as there is generics)
 - remove unecessary stubbing (mockito is now reporting un-needed stubs)

Change-Id: I69c7ccc86bc85a4401d5e33671229ae0ce560dbb
Signed-off-by: Florent BENOIT <fbenoit@redhat.com>
  • Loading branch information
benoitf committed Oct 15, 2017
1 parent 891b619 commit 9581a9b
Show file tree
Hide file tree
Showing 82 changed files with 588 additions and 558 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/
package org.eclipse.che.api.agent.server.launcher;

import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -62,8 +62,9 @@ public void setUp() throws Exception {
when(agent.getScript()).thenReturn("script content");
doReturn(process)
.when(launcher)
.start(any(Instance.class), any(Agent.class), any(LineConsumer.class));
when(agentChecker.isLaunched(any(Agent.class), any(InstanceProcess.class), any(Instance.class)))
.start(nullable(Instance.class), nullable(Agent.class), nullable(LineConsumer.class));
when(agentChecker.isLaunched(
nullable(Agent.class), nullable(InstanceProcess.class), nullable(Instance.class)))
.thenReturn(true);
when(machine.getNode()).thenReturn(mock(InstanceNode.class));
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public void shouldNotCheckIfAgentIsLaunchedMoreThanAgentMaxStartTime() throws Ex
launcher = spy(new TestAgentLauncher(200, 100, agentChecker));
doReturn(process)
.when(launcher)
.start(any(Instance.class), any(Agent.class), any(LineConsumer.class));
.start(nullable(Instance.class), nullable(Agent.class), nullable(LineConsumer.class));
when(agentChecker.isLaunched(any(Agent.class), any(InstanceProcess.class), any(Instance.class)))
.thenReturn(false)
.thenReturn(false)
Expand All @@ -158,7 +159,7 @@ public void shouldNotCheckMoreFrequentThanAgentCheckDelay() throws Exception {
launcher = spy(new TestAgentLauncher(200, 10, agentChecker));
doReturn(process)
.when(launcher)
.start(any(Instance.class), any(Agent.class), any(LineConsumer.class));
.start(nullable(Instance.class), nullable(Agent.class), nullable(LineConsumer.class));
// record time of each check of agent state
ArrayList<Long> checkTimestamps = new ArrayList<>(5);
Answer<Boolean> recordTimestampAndReturnFalse =
Expand Down Expand Up @@ -253,8 +254,9 @@ public void shouldStartMachineProcessWithAgentScriptExecution() throws Exception
String agentScript = "testAgentScript";
when(agent.getId()).thenReturn(agentId);
when(agent.getScript()).thenReturn(agentScript);
when(launcher.start(any(Instance.class), any(Agent.class), any(LineConsumer.class)))
.thenCallRealMethod();
doCallRealMethod()
.when(launcher)
.start(nullable(Instance.class), nullable(Agent.class), nullable(LineConsumer.class));

// when
launcher.launch(machine, agent);
Expand All @@ -275,15 +277,17 @@ public void shouldLogAgentStartLogsIfTimeoutReached() throws Exception {

doReturn(process)
.when(launcher)
.start(any(Instance.class), any(Agent.class), any(LineConsumer.class));
.start(nullable(Instance.class), nullable(Agent.class), nullable(LineConsumer.class));

// when
try {
launcher.launch(machine, agent);
fail("Should throw AgentStartException");
} catch (AgentStartException e) {
// then
verify(launcher).logAsErrorAgentStartLogs(anyObject(), anyString(), anyString());
verify(launcher)
.logAsErrorAgentStartLogs(
nullable(Instance.class), nullable(String.class), nullable(String.class));
// rethrow exception to verify message
throw e;
}
Expand All @@ -297,15 +301,17 @@ public void shouldLogAgentStartLogsIfMachineExceptionOccurs() throws Exception {
// given
doThrow(new MachineException("An error on agent start"))
.when(launcher)
.start(any(Instance.class), any(Agent.class), any(LineConsumer.class));
.start(nullable(Instance.class), nullable(Agent.class), nullable(LineConsumer.class));

// when
try {
launcher.launch(machine, agent);
fail("Should throw ServerException");
verify(launcher)
.logAsErrorAgentStartLogs(
nullable(Instance.class), nullable(String.class), nullable(String.class));
} catch (ServerException e) {
// then
verify(launcher).logAsErrorAgentStartLogs(anyObject(), anyString(), anyString());
// rethrow exception to verify message
throw e;
}
Expand All @@ -324,7 +330,7 @@ public void shouldLogAgentStartLogsIfMachineExceptionOccursAfterAgentStartTimeou

doReturn(process)
.when(launcher)
.start(any(Instance.class), any(Agent.class), any(LineConsumer.class));
.start(nullable(Instance.class), nullable(Agent.class), nullable(LineConsumer.class));
doThrow(new MachineException("An error on process kill")).when(process).kill();

// when
Expand All @@ -333,7 +339,9 @@ public void shouldLogAgentStartLogsIfMachineExceptionOccursAfterAgentStartTimeou
fail("Should throw ServerException");
} catch (ServerException e) {
// then
verify(launcher).logAsErrorAgentStartLogs(anyObject(), anyString(), anyString());
verify(launcher)
.logAsErrorAgentStartLogs(
nullable(Instance.class), nullable(String.class), nullable(String.class));
// rethrow exception to verify message
throw e;
}
Expand Down
2 changes: 1 addition & 1 deletion assembly/assembly-wsmaster-war/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
5 changes: 0 additions & 5 deletions core/che-core-api-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -75,6 +76,8 @@ public void shouldDispatchResponseIfResponseReceived() throws Exception {
when(jsonRpcQualifier.isJsonRpcResponse(MESSAGE)).thenReturn(true);
when(jsonRpcQualifier.isJsonRpcRequest(MESSAGE)).thenReturn(false);
when(jsonRpcUnmarshaller.unmarshalArray(any())).thenReturn(singletonList(MESSAGE));
JsonRpcResponse jsonRpcResponse = Mockito.mock(JsonRpcResponse.class);
when(jsonRpcUnmarshaller.unmarshalResponse(any())).thenReturn(jsonRpcResponse);

jsonRpcMessageReceiver.receive(ENDPOINT_ID, MESSAGE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.eclipse.che.api.core.util.LinksHelper.createLink;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
Expand Down Expand Up @@ -98,8 +97,13 @@ public void shouldUseUrlAndMethodFromTheLinks() throws Exception {
final DefaultHttpJsonRequest request = spy(new DefaultHttpJsonRequest(link));
doReturn(new DefaultHttpJsonResponse("", 200))
.when(request)
.doRequest(anyInt(), anyString(), anyString(), anyObject(), any(), anyString());

.doRequest(
anyInt(),
anyString(),
anyString(),
nullable(Object.class),
nullable(List.class),
nullable(String.class));
request.request();

verify(request).doRequest(0, DEFAULT_URL, "POST", null, null, null);
Expand Down Expand Up @@ -365,6 +369,12 @@ private String getUrl(ITestContext ctx) {
private void prepareResponse(String response) throws Exception {
doReturn(new DefaultHttpJsonResponse(response, 200))
.when(request)
.doRequest(anyInt(), anyString(), anyString(), anyObject(), any(), anyString());
.doRequest(
anyInt(),
anyString(),
anyString(),
nullable(Object.class),
nullable(List.class),
nullable(String.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.mockito.Mock;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.verification.VerificationModeFactory;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.invocation.Invocation;
import org.mockito.testng.MockitoTestNGListener;
import org.mockito.verification.VerificationMode;
Expand Down Expand Up @@ -260,8 +262,10 @@ private LineConsumer[] appendTo(LineConsumer[] base, LineConsumer... toAppend) {
* verify(someMock, last()).someMethod();
* </code></pre>
*/
private static VerificationMode last() {
return (verificationData) -> {
public static class Last implements VerificationMode {
public Last() {}

public void verify(VerificationData verificationData) {
List<Invocation> invocations = verificationData.getAllInvocations();
InvocationMatcher invocationMatcher = verificationData.getWanted();

Expand All @@ -276,6 +280,14 @@ private static VerificationMode last() {
if (!invocationMatcher.matches(invocation)) {
throw new MockitoException("\nWanted but not invoked:\n" + invocationMatcher);
}
};
}

public VerificationMode description(String description) {
return VerificationModeFactory.description(this, description);
}
}

public static VerificationMode last() {
return new Last();
}
}
2 changes: 1 addition & 1 deletion core/che-core-typescript-dto-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
7 changes: 0 additions & 7 deletions core/commons/che-core-commons-inject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,9 @@
<artifactId>che-core-commons-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
5 changes: 0 additions & 5 deletions core/commons/che-core-commons-lang/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ide/che-core-dyna-provider-generator-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ public void shouldResortAllActionsAfterAddingOne() {
when(actionManager.getId(eq(secondAction))).thenReturn("secondAction");

defaultActionGroup.add(thirdAction, new Constraints(AFTER, "fourthAction"));
when(actionManager.getId(eq(thirdAction))).thenReturn("thirdAction");

// verify order
Action[] result = defaultActionGroup.getChildren(mock(ActionEvent.class));
Expand All @@ -371,7 +370,6 @@ public void shouldResortAllActionsAfterAddingOne() {

// add other actions
defaultActionGroup.add(fourthAction);
when(actionManager.getId(eq(thirdAction))).thenReturn("thirdAction");

defaultActionGroup.add(fifthAction, Constraints.FIRST);
when(actionManager.getId(eq(fifthAction))).thenReturn("fifthAction");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ public void testFirstLineNoLeadingSpaces() {

@Test
public void testStartNotEmptyLine() {
doReturn("whatever").when(document).getLineContent(0);
doReturn("s/*").when(document).getLineContent(1);
doReturn(" *").when(document).getLineContent(2);
final TextChange input =
new TextChange.Builder()
.from(new TextPosition(1, 3))
Expand Down Expand Up @@ -173,8 +171,6 @@ public void testJavadocStyleComment() {

@Test
public void testPasteWholeCommentStart() {
doReturn("/**").when(document).getLineContent(0);
doReturn(" *").when(document).getLineContent(1);
final TextChange input =
new TextChange.Builder()
.from(new TextPosition(0, 0))
Expand All @@ -187,8 +183,6 @@ public void testPasteWholeCommentStart() {

@Test
public void testCloseComment() {
doReturn("/**").when(document).getLineContent(0);
doReturn(" *").when(document).getLineContent(1);
final TextChange input =
new TextChange.Builder()
.from(new TextPosition(0, 0))
Expand Down
Loading

0 comments on commit 9581a9b

Please sign in to comment.