Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Add rule to verify static imports of Mockito
Browse files Browse the repository at this point in the history
and for Assertions as well.

Signed-off-by: Moti Asayag <[email protected]>
  • Loading branch information
masayag authored and openshift-merge-robot committed May 30, 2023
1 parent 893e487 commit 0ba2648
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 27 deletions.
13 changes: 13 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,18 @@
<property name="illegalPattern" value="true"/>
<property name="message" value="Trailing whitespace"/>
</module>

<!-- Use static import for mockito -->
<module name="Regexp">
<property name="format" value="^(?!import).*Mockito\."/>
<property name="illegalPattern" value="true"/>
<property name="message" value="Use static import for Mockito"/>
</module>

<module name="Regexp">
<property name="format" value="^(?!import).*Assertions\."/>
<property name="illegalPattern" value="true"/>
<property name="message" value="Use static import for Assertions"/>
</module>
</module>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static com.redhat.parodos.tasks.migrationtoolkit.TestConsts.APP_ID;
import static com.redhat.parodos.tasks.migrationtoolkit.TestConsts.APP_NAME;
import static com.redhat.parodos.tasks.migrationtoolkit.TestConsts.REPO_URL;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -51,7 +51,7 @@ public void missingMandatoryParameters() {
assertThat(execute.getError()).isInstanceOf(MissingParameterException.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
assertThat(execute.getWorkContext().get("application")).isNull();
verify(mockClient, Mockito.times(0)).create(any());
verify(mockClient, times(0)).create(any());
}

@Test
Expand All @@ -68,7 +68,7 @@ public void createFails() {
assertThat(execute.getError()).isInstanceOf(Exception.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
assertThat(execute.getWorkContext().get("application")).isNull();
verify(mockClient, Mockito.times(1)).create(any());
verify(mockClient, times(1)).create(any());
}

@Test
Expand All @@ -91,7 +91,7 @@ public void createCompletes() {

// 0 is wanted explicitly because it is an empty ID for the server request. (IDs
// are generated by the server)
verify(mockClient, Mockito.times(1)).create(new App(0, APP_NAME, new Repository("git", REPO_URL)));
verify(mockClient, times(1)).create(new App(0, APP_NAME, new Repository("git", REPO_URL)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static com.redhat.parodos.tasks.migrationtoolkit.TestConsts.APP_ID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -51,7 +51,7 @@ public void missingMandatoryParams() {
assertThat(execute.getError()).isInstanceOf(MissingParameterException.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
assertThat(execute.getWorkContext().get("taskGroupID")).isNull();
verify(mockClient, Mockito.times(0)).get(anyInt());
verify(mockClient, times(0)).get(anyInt());
}

@Test
Expand All @@ -68,7 +68,7 @@ public void failsGetTaskGroup() {
assertThat(execute.getError()).isNotInstanceOf(MissingParameterException.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
assertThat(execute.getWorkContext().get("taskGroupID")).isEqualTo("123");
verify(mockClient, Mockito.times(1)).get(anyInt());
verify(mockClient, times(1)).get(anyInt());
}

@Test
Expand All @@ -85,8 +85,8 @@ public void getByID() {
assertThat(execute.getError()).isNull();
assertThat(execute.getStatus()).isEqualTo(WorkStatus.COMPLETED);
assertThat(execute.getWorkContext().getEntrySet()).contains(entry("taskGroupID", taskGroupID));
verify(mockClient, Mockito.times(1)).get(eq(Integer.parseInt(taskGroupID)));
verify(mockClient, Mockito.times(0)).create(anyInt());
verify(mockClient, times(1)).get(eq(Integer.parseInt(taskGroupID)));
verify(mockClient, times(0)).create(anyInt());
}

static TaskGroup successfulGet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static com.redhat.parodos.tasks.migrationtoolkit.TestConsts.APP_ID;
Expand All @@ -22,6 +21,7 @@
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -52,7 +52,7 @@ public void missingMandatoryParams() {

assertThat(execute.getError()).isInstanceOf(MissingParameterException.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
verify(mockClient, Mockito.times(0)).get(anyString());
verify(mockClient, times(0)).get(anyString());
}

@Test
Expand All @@ -68,7 +68,7 @@ public void failsGettingAppNotFound() {
assertThat(execute.getError()).isInstanceOf(Exception.class);
assertThat(execute.getError()).isNotInstanceOf(MissingParameterException.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
verify(mockClient, Mockito.times(1)).get(anyString());
verify(mockClient, times(1)).get(anyString());
}

@Test
Expand All @@ -85,8 +85,8 @@ public void getByName() {
assertThat(execute.getError()).isNull();
assertThat(execute.getStatus()).isEqualTo(WorkStatus.COMPLETED);
assertThat(execute.getWorkContext().getEntrySet()).contains(entry("applicationID", APP_ID));
verify(mockClient, Mockito.times(1)).get(anyString());
verify(mockClient, Mockito.times(0)).create(any());
verify(mockClient, times(1)).get(anyString());
verify(mockClient, times(0)).create(any());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static com.redhat.parodos.workflows.workflow.WorkContextAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -50,7 +50,7 @@ public void missingMandatoryParams() {
assertThat(execute.getError()).isInstanceOf(MissingParameterException.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
assertThat(execute.getWorkContext().get("taskGroup")).isNull();
verify(mockClient, Mockito.times(0)).create(anyInt());
verify(mockClient, times(0)).create(anyInt());
}

@Test
Expand All @@ -67,7 +67,7 @@ public void failsCreatingTaskGroup() {
assertThat(execute.getError()).isInstanceOf(Exception.class);
assertThat(execute.getStatus()).isEqualTo(WorkStatus.FAILED);
assertThat(execute.getWorkContext().get("taskGroup")).isNull();
verify(mockClient, Mockito.times(1)).create(anyInt());
verify(mockClient, times(1)).create(anyInt());
}

@Test
Expand All @@ -86,7 +86,7 @@ public void createCompletes() {
assertThat(execute.getStatus()).isEqualTo(WorkStatus.COMPLETED);
assertThat(execute.getWorkContext()).hasEntryKey("analysisTaskGroup");
assertThat(((TaskGroup) execute.getWorkContext().get("analysisTaskGroup")).id()).isEqualTo(taskGroupID);
verify(mockClient, Mockito.times(1)).create(appID);
verify(mockClient, times(1)).create(appID);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.redhat.parodos.workflows.work.WorkStatus;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand All @@ -26,10 +25,11 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

public class RestWorkFlowTaskTest {

private RestService restService = Mockito.mock(RestService.class);
private RestService restService = mock(RestService.class);

private RestWorkFlowTask task = new RestWorkFlowTask("Test", restService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;

import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -72,8 +73,8 @@ void execute_when_mainWorkflowIsFailed_and_rollbackIsFound_then_executeRollback(
.rollbackWorkFlowName(rollbackWorkflowName).build();
workFlowExecutor.execute(executionContext);

verify(mainWorkFlow, Mockito.times(1)).execute(any(WorkContext.class));
verify(rollbackWorkFlow, Mockito.times(1)).execute(any(WorkContext.class));
verify(mainWorkFlow, times(1)).execute(any(WorkContext.class));
verify(rollbackWorkFlow, times(1)).execute(any(WorkContext.class));

}

Expand All @@ -84,8 +85,8 @@ void execute_when_mainWorkflowIsFailed_and_rollbackIsNotFound_then_notExecuteRol
.workFlowName(workflowName).workContext(workContext).executionId(executionId).build();
workFlowExecutor.execute(executionContext);

verify(mainWorkFlow, Mockito.times(1)).execute(any(WorkContext.class));
verify(rollbackWorkFlow, Mockito.never()).execute(any(WorkContext.class));
verify(mainWorkFlow, times(1)).execute(any(WorkContext.class));
verify(rollbackWorkFlow, never()).execute(any(WorkContext.class));
}

@Test
Expand All @@ -96,8 +97,8 @@ void execute_when_mainWorkflowIsCompleted_then_notExecuteRollback() {
.rollbackWorkFlowName(rollbackWorkflowName).build();
workFlowExecutor.execute(executionContext);

verify(mainWorkFlow, Mockito.times(1)).execute(any(WorkContext.class));
verify(rollbackWorkFlow, Mockito.never()).execute(any(WorkContext.class));
verify(mainWorkFlow, times(1)).execute(any(WorkContext.class));
verify(rollbackWorkFlow, never()).execute(any(WorkContext.class));
}

}

0 comments on commit 0ba2648

Please sign in to comment.