diff --git a/.ci.cambpm b/.ci.cambpm index 0a8bc618..40cf02a7 100644 --- a/.ci.cambpm +++ b/.ci.cambpm @@ -9,6 +9,7 @@ buildMavenAndDeployToMavenCentral([ 'jdk-12-latest', 'jdk-13-latest', 'jdk-14-latest', - 'jdk-15-latest' + 'jdk-15-latest', + 'jdk-17-latest' ] ]) diff --git a/core/src/test/java/org/camunda/bpm/engine/test/assertions/bpmn/ProcessEngineTestsTest.java b/core/src/test/java/org/camunda/bpm/engine/test/assertions/bpmn/ProcessEngineTestsTest.java index 1c9eeade..f48edcdf 100644 --- a/core/src/test/java/org/camunda/bpm/engine/test/assertions/bpmn/ProcessEngineTestsTest.java +++ b/core/src/test/java/org/camunda/bpm/engine/test/assertions/bpmn/ProcessEngineTestsTest.java @@ -30,10 +30,9 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; @@ -48,21 +47,23 @@ /** * @author Martin Schimak (martin.schimak@plexiti.com) */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(ProcessEngines.class) +@RunWith(MockitoJUnitRunner.class) public class ProcessEngineTestsTest { ProcessEngine processEngine; + MockedStatic processEnginesMockedStatic; @Before public void setUp() { processEngine = mock(ProcessEngine.class); + processEnginesMockedStatic = mockStatic(ProcessEngines.class, CALLS_REAL_METHODS); init(processEngine); } @After public void tearDown() { reset(); + processEnginesMockedStatic.close(); } @Test @@ -72,12 +73,11 @@ public void testProcessEngine() throws Exception { // Then assertThat(returnedEngine).isNotNull().isSameAs(processEngine); } - + @Test public void testNoProcessEngine_Failure() throws Exception { // Given - PowerMockito.spy(ProcessEngines.class); - Mockito.when(ProcessEngines.getProcessEngines()).thenReturn(new HashMap()); + processEnginesMockedStatic.when(ProcessEngines::getProcessEngines).thenReturn(new HashMap()); reset(); try { // When @@ -88,15 +88,14 @@ public void testNoProcessEngine_Failure() throws Exception { assertThat(e).hasMessage("No ProcessEngine found to be registered with ProcessEngines!"); } } - + @Test public void testMultipleProcessEngine_Failure() throws Exception { // Given Map multipleEnginesMap = new HashMap<>(); multipleEnginesMap.put("test1", mock(ProcessEngine.class)); multipleEnginesMap.put("test2", mock(ProcessEngine.class)); - PowerMockito.spy(ProcessEngines.class); - Mockito.when(ProcessEngines.getProcessEngines()).thenReturn(multipleEnginesMap); + processEnginesMockedStatic.when(ProcessEngines::getProcessEngines).thenReturn(multipleEnginesMap); reset(); try { // When @@ -173,7 +172,7 @@ public void testAssertThat_Job() throws Exception { JobAssert jobAssert = assertThat(job); assertThat(jobAssert.getActual()).isSameAs(job); } - + @Test public void testAssertThat_CaseInstance() throws Exception { //Given @@ -367,7 +366,7 @@ public void testProcessDefinitionQuery() { verify(repositoryService, times(1)).createProcessDefinitionQuery(); verifyNoMoreInteractions(repositoryService); } - + @Test public void testExecutionQuery() { // Given diff --git a/core/src/test/java/org/camunda/bpm/engine/test/assertions/cmmn/CmmnAwareTestsTest.java b/core/src/test/java/org/camunda/bpm/engine/test/assertions/cmmn/CmmnAwareTestsTest.java index 9e0a5c2e..958cc57f 100644 --- a/core/src/test/java/org/camunda/bpm/engine/test/assertions/cmmn/CmmnAwareTestsTest.java +++ b/core/src/test/java/org/camunda/bpm/engine/test/assertions/cmmn/CmmnAwareTestsTest.java @@ -17,14 +17,9 @@ package org.camunda.bpm.engine.test.assertions.cmmn; import static org.camunda.bpm.engine.test.assertions.cmmn.CmmnAwareTests.withVariables; -import static org.camunda.bpm.engine.test.assertions.helpers.CamundaMatchers.*; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.*; import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.*; -import static org.powermock.api.mockito.PowerMockito.verifyNoMoreInteractions; import org.assertj.core.api.Assertions; import org.camunda.bpm.engine.CaseService; @@ -35,17 +30,17 @@ import org.camunda.bpm.engine.runtime.*; import org.camunda.bpm.engine.test.assertions.bpmn.AbstractAssertions; import org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests; -import org.camunda.bpm.engine.test.assertions.helpers.CamundaMatchers; import org.camunda.bpm.engine.test.assertions.helpers.CaseExecutionQueryFluentAnswer; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; /** * You will notice that this test class does not cover all methods. @@ -54,12 +49,10 @@ * * @author Malte Sörensen (malte.soerensen@holisticon.de) */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({BpmnAwareTests.class, CmmnAwareTests.class, AbstractAssertions.class}) +@RunWith(MockitoJUnitRunner.class) public class CmmnAwareTestsTest { public static final String ACTIVITY_ID = "FOO"; - public static final String CASE_INSTANCE_ID = "BAR"; @Rule public ExpectedException thrown = ExpectedException.none(); @@ -73,26 +66,52 @@ public class CmmnAwareTestsTest { @Mock private CaseDefinition caseDefinition; + @Mock + private ProcessEngine processEngine; + + @Mock + private CaseService caseService; + //mocked below private CaseExecutionQuery caseExecutionQuery; + private MockedStatic cmmnAwareTestsMockedStatic; + private MockedStatic abstractAssertionsMockedStatic; + + @Before + public void setUp() { + cmmnAwareTestsMockedStatic = mockStatic(CmmnAwareTests.class, CALLS_REAL_METHODS); + abstractAssertionsMockedStatic = mockStatic(AbstractAssertions.class); + abstractAssertionsMockedStatic.when(AbstractAssertions::processEngine).thenReturn(processEngine); + when(processEngine.getCaseService()).thenReturn(caseService); + + caseExecutionQuery = mock(CaseExecutionQuery.class, new CaseExecutionQueryFluentAnswer()); + when(caseService.createCaseExecutionQuery()).thenReturn(caseExecutionQuery); + } + + @After + public void tearDown() { + cmmnAwareTestsMockedStatic.close(); + abstractAssertionsMockedStatic.close(); + } @Test public void assertThatCaseDefinition_should_delegate_to_CmmnAwareAssertions() { //prepare and mock static methods //because we need control over the CaseDefinitionAssert created - mockStatic(CmmnAwareTests.class); + final MockedStatic caseDefinitionAssertMockedStatic = mockStatic(CaseDefinitionAssert.class); CaseDefinitionAssert caseDefinitionAssert = mock(CaseDefinitionAssert.class); - when(CmmnAwareTests.assertThat(any(CaseDefinition.class))).thenReturn(caseDefinitionAssert); + caseDefinitionAssertMockedStatic.when(() -> CaseDefinitionAssert.assertThat(Mockito.any(), any())).thenReturn(caseDefinitionAssert); //when calling the method under test with a non-null CaseDefinition object CaseDefinitionAssert actualCaseDefinitionAssert = CmmnAwareTests.assertThat(caseDefinition); //then the delegate is called with that CaseDefinition object - verifyStatic(CmmnAwareTests.class); - CmmnAwareTests.assertThat(caseDefinition); + caseDefinitionAssertMockedStatic.verify(() -> CaseDefinitionAssert.assertThat(Mockito.any(), any())); //and whatever the delegate returns, is returned by the tested method, too Assertions.assertThat(actualCaseDefinitionAssert).isSameAs(caseDefinitionAssert); + + caseDefinitionAssertMockedStatic.close(); } @Test @@ -100,18 +119,19 @@ public void assertThatCaseExecution_should_delegate_to_CmmnAwareAssertions() { //prepare and mock static methods //because we need control over the CaseExecutionAssert created - mockStatic(CmmnAwareTests.class); + final MockedStatic caseExecutionAssertMockedStatic = mockStatic(CaseExecutionAssert.class); CaseExecutionAssert caseExecutionAssert = mock(CaseExecutionAssert.class); - when(CmmnAwareTests.assertThat(anyCaseExecution())).thenReturn(caseExecutionAssert); + caseExecutionAssertMockedStatic.when(() -> CaseExecutionAssert.assertThat(Mockito.any(), any())).thenReturn(caseExecutionAssert); //when calling the method under test with a non-null CaseExecution object CaseExecutionAssert actualCaseExecutionAssert = CmmnAwareTests.assertThat(caseExecution); //then the delegate is called with that CaseExecution object - verifyStatic(CmmnAwareTests.class); - CmmnAwareTests.assertThat(caseExecution); + caseExecutionAssertMockedStatic.verify(() -> CaseExecutionAssert.assertThat(Mockito.any(), any())); //and whatever the delegate returns, is returned by the tested method, too Assertions.assertThat(actualCaseExecutionAssert).isSameAs(caseExecutionAssert); + + caseExecutionAssertMockedStatic.close(); } @Test @@ -119,18 +139,19 @@ public void assertThatCaseInstance_should_delegate_to_CmmnAwareAssertions() { //prepare and mock static methods //because we need control over the CaseInstanceAssert created - mockStatic(CmmnAwareTests.class); + final MockedStatic caseInstanceAssertMockedStatic = mockStatic(CaseInstanceAssert.class); CaseInstanceAssert caseInstanceAssert = mock(CaseInstanceAssert.class); - when(CmmnAwareTests.assertThat(anyCaseInstance())).thenReturn(caseInstanceAssert); + caseInstanceAssertMockedStatic.when(() -> CaseInstanceAssert.assertThat(any(), any())).thenReturn(caseInstanceAssert); //when calling the method under test with a non-null CaseInstance object CaseInstanceAssert actualCaseInstanceAssert = CmmnAwareTests.assertThat(caseInstance); //then the delegate is called with that CaseInstance object - verifyStatic(CmmnAwareTests.class); - CmmnAwareTests.assertThat(caseInstance); + caseInstanceAssertMockedStatic.verify(() -> CaseInstanceAssert.assertThat(any(), any())); //and whatever the delegate returns, is returned by the tested method, too Assertions.assertThat(actualCaseInstanceAssert).isSameAs(caseInstanceAssert); + + caseInstanceAssertMockedStatic.close(); } @Test @@ -138,9 +159,9 @@ public void caseDefinitionQuery_should_create_vanilla_query() { //prepare and mock static methods //because we need control over the CaseDefinitionQuery created - mockStatic(BpmnAwareTests.class); + final MockedStatic bpmnAwareTestsMockedStatic = mockStatic(BpmnAwareTests.class); RepositoryService repositoryService = mock(RepositoryService.class); - when(BpmnAwareTests.repositoryService()).thenReturn(repositoryService); + bpmnAwareTestsMockedStatic.when(BpmnAwareTests::repositoryService).thenReturn(repositoryService); CaseDefinitionQuery caseDefinitionQuery = mock(CaseDefinitionQuery.class); when(repositoryService.createCaseDefinitionQuery()).thenReturn(caseDefinitionQuery); @@ -154,41 +175,37 @@ public void caseDefinitionQuery_should_create_vanilla_query() { Assertions.assertThat(actualCaseDefinitionQuery).isSameAs(caseDefinitionQuery); //and the returned CaseDefinitionQuery is a vanilla one verifyNoMoreInteractions(caseDefinitionQuery); + + bpmnAwareTestsMockedStatic.close(); } @Test public void caseExecutionCaseExecutionQueryCaseInstance_should_delegate_to_assertThatCaseInstance() { + final MockedStatic caseInstanceAssertMockedStatic = mockStatic(CaseInstanceAssert.class); CaseInstanceAssert caseInstanceAssert = mock(CaseInstanceAssert.class); when(caseInstanceAssert.isNotNull()).thenReturn(caseInstanceAssert); when(caseInstanceAssert.descendantCaseExecution(caseExecutionQuery)).thenReturn(mock(CaseExecutionAssert.class)); //prepare and mock static methods //because we need control over the context of tested method - mockStatic(CmmnAwareTests.class); - when(CmmnAwareTests.assertThat(anyCaseInstance())).thenReturn(caseInstanceAssert); - when(CmmnAwareTests.caseExecution(anyCaseExecutionQuery(), anyCaseInstance())).thenCallRealMethod(); + caseInstanceAssertMockedStatic.when(() -> CaseInstanceAssert.assertThat(any(), any())).thenReturn(caseInstanceAssert); //when getting the CaseExecution for a CaseInstance and a given CaseExecutionQuery CmmnAwareTests.caseExecution(caseExecutionQuery, caseInstance); //then the call is delegated via assertThat to a CaseInstanceAssert - verifyStatic(CmmnAwareTests.class); - CmmnAwareTests.assertThat(caseInstance); + cmmnAwareTestsMockedStatic.verify(() -> CmmnAwareTests.assertThat(caseInstance)); + caseInstanceAssertMockedStatic.verify(() -> CaseInstanceAssert.assertThat(any(), any())); //and the CaseExecutionQuery is used for narrowing down the result verify(caseInstanceAssert).descendantCaseExecution(caseExecutionQuery); verifyNoMoreInteractions(caseExecutionQuery); + + caseInstanceAssertMockedStatic.close(); } @Test public void caseExecutionQuery_should_create_vanilla_query() { - - //prepare and mock static methods - //because we need control over the CaseExecutionQuery created - mockStatic(CmmnAwareTests.class); - when(CmmnAwareTests.caseExecutionQuery()).thenCallRealMethod(); - CaseService caseService = mock(CaseService.class); - when(CmmnAwareTests.caseService()).thenReturn(caseService); when(caseService.createCaseExecutionQuery()).thenReturn(caseExecutionQuery); //when getting a CaseExecutionQuery @@ -204,34 +221,29 @@ public void caseExecutionQuery_should_create_vanilla_query() { @Test public void caseExecutionStringCaseInstance_should_delegate_to_its_query_variant() { - //prepare and mock static methods - //because we need control over the context of tested method - mockStatic(CmmnAwareTests.class); - when(CmmnAwareTests.caseExecution(anyString(), anyCaseInstance())).thenCallRealMethod(); - when(CmmnAwareTests.caseExecution(anyCaseExecutionQuery(), anyCaseInstance())).thenReturn(caseExecution); - when(CmmnAwareTests.caseExecutionQuery()).thenReturn(caseExecutionQuery); + final MockedStatic caseInstanceAssertMockedStatic = mockStatic(CaseInstanceAssert.class); + final CaseInstanceAssert caseInstanceAssert = mock(CaseInstanceAssert.class); + caseInstanceAssertMockedStatic.when(() -> CaseInstanceAssert.assertThat(any(), eq(caseInstance))).thenReturn(caseInstanceAssert); + when(caseInstanceAssert.isNotNull()).thenReturn(caseInstanceAssert); + final CaseExecutionAssert caseExecutionAssert = mock(CaseExecutionAssert.class); + when(caseInstanceAssert.descendantCaseExecution(any())).thenReturn(caseExecutionAssert); + when(caseExecutionAssert.getActual()).thenReturn(caseExecution); //when getting the CaseExecution for activity FOO in a given case instance CmmnAwareTests.caseExecution(ACTIVITY_ID, caseInstance); //then the caseExecutionQuery is enhanced with the activityId only verify(caseExecutionQuery).activityId(ACTIVITY_ID); + caseInstanceAssertMockedStatic.verify(() -> CaseInstanceAssert.assertThat(any(), eq(caseInstance))); verifyNoMoreInteractions(caseExecutionQuery); //and the call is delegated properly - verifyStatic(CmmnAwareTests.class); - CmmnAwareTests.caseExecution(caseExecutionQuery, caseInstance); + cmmnAwareTestsMockedStatic.verify(() -> CmmnAwareTests.caseExecution(caseExecutionQuery, caseInstance)); + + caseInstanceAssertMockedStatic.close(); } @Test public void caseInstanceQuery_should_create_vanilla_query() { - - //prepare and mock static methods - //because we need control over the CaseInstanceQuery created - mockStatic(CmmnAwareTests.class); - when(CmmnAwareTests.caseInstanceQuery()).thenCallRealMethod(); - CaseService caseService = mock(CaseService.class); - when(CmmnAwareTests.caseService()).thenReturn(caseService); - CaseInstanceQuery caseInstanceQuery = mock(CaseInstanceQuery.class); when(caseService.createCaseInstanceQuery()).thenReturn(caseInstanceQuery); @@ -248,15 +260,6 @@ public void caseInstanceQuery_should_create_vanilla_query() { @Test public void caseService_should_return_the_processEngines_caseService() { - - //prepare and mock static methods - //because we need control over the CaseService created - mockStatic(AbstractAssertions.class); - ProcessEngine processEngine = mock(ProcessEngine.class); - when(AbstractAssertions.processEngine()).thenReturn(processEngine); - CaseService caseService = mock(CaseService.class); - when(processEngine.getCaseService()).thenReturn(caseService); - //when getting a CaseInstanceQuery CaseService actualCaseService = CmmnAwareTests.caseService(); @@ -266,14 +269,6 @@ public void caseService_should_return_the_processEngines_caseService() { @Test public void completeCaseExecution_should_delegate_to_caseService() { - //prepare and mock static methods - //because we need control over the CaseService created - mockStatic(AbstractAssertions.class); - ProcessEngine processEngine = mock(ProcessEngine.class); - when(AbstractAssertions.processEngine()).thenReturn(processEngine); - CaseService caseService = mock(CaseService.class); - when(processEngine.getCaseService()).thenReturn(caseService); - when(caseExecution.getId()).thenReturn("baz"); CmmnAwareTests.complete(caseExecution); @@ -290,14 +285,6 @@ public void completeCaseExecution_should_throw_IAE_for_null_arg() { @Test public void disableCaseExecution_should_delegate_to_caseService() { - //prepare and mock static methods - //because we need control over the CaseService created - mockStatic(AbstractAssertions.class); - ProcessEngine processEngine = mock(ProcessEngine.class); - when(AbstractAssertions.processEngine()).thenReturn(processEngine); - CaseService caseService = mock(CaseService.class); - when(processEngine.getCaseService()).thenReturn(caseService); - when(caseExecution.getId()).thenReturn("baz"); CmmnAwareTests.disable(caseExecution); @@ -314,14 +301,6 @@ public void disableCaseExecution_should_throw_IAE_for_null_arg() { @Test public void manuallyStartCaseExecution_should_delegate_to_caseService() { - //prepare and mock static methods - //because we need control over the CaseService created - mockStatic(AbstractAssertions.class); - ProcessEngine processEngine = mock(ProcessEngine.class); - when(AbstractAssertions.processEngine()).thenReturn(processEngine); - CaseService caseService = mock(CaseService.class); - when(processEngine.getCaseService()).thenReturn(caseService); - when(caseExecution.getId()).thenReturn("baz"); CmmnAwareTests.manuallyStart(caseExecution); @@ -338,16 +317,6 @@ public void manuallyStartCaseExecution_should_throw_IAE_for_null_arg() { @Test public void completeCaseExecutionWithVariables_should_delegate_to_caseService() { - //prepare and mock static methods - //because we need control over the CaseService created - mockStatic(AbstractAssertions.class); - ProcessEngine processEngine = mock(ProcessEngine.class); - when(AbstractAssertions.processEngine()).thenReturn(processEngine); - CaseService caseService = mock(CaseService.class); - when(processEngine.getCaseService()).thenReturn(caseService); - CaseExecutionCommandBuilder caseExecutionCommandBuilder = mock(CaseExecutionCommandBuilder.class); - when(caseService.withCaseExecution("baz")).thenReturn(caseExecutionCommandBuilder); - when(caseExecutionCommandBuilder.setVariables(CamundaMatchers.anyMap())).thenReturn(caseExecutionCommandBuilder); when(caseExecution.getId()).thenReturn("baz"); CmmnAwareTests.complete(caseExecution, withVariables("aVariable", "aValue")); @@ -369,14 +338,4 @@ public void completeCaseExecutionWithVariables_should_throw_IAE_for_null_arg_val CmmnAwareTests.complete(caseExecution, null); } - @Before - public void prepareCaseExecutionQuery() { - caseExecutionQuery = mock(CaseExecutionQuery.class, new CaseExecutionQueryFluentAnswer()); - when(caseExecutionQuery.toString()).thenCallRealMethod(); - } - - @Before - public void prepareCaseInstance() { - when(caseInstance.getId()).thenReturn(CASE_INSTANCE_ID); - } -} \ No newline at end of file +} diff --git a/examples/bpmn/src/test/java/org/camunda/bpm/engine/test/assertions/examples/JobAnnouncementPublicationProcessTest.java b/examples/bpmn/src/test/java/org/camunda/bpm/engine/test/assertions/examples/JobAnnouncementPublicationProcessTest.java index 99dbe86a..eb2d6c36 100644 --- a/examples/bpmn/src/test/java/org/camunda/bpm/engine/test/assertions/examples/JobAnnouncementPublicationProcessTest.java +++ b/examples/bpmn/src/test/java/org/camunda/bpm/engine/test/assertions/examples/JobAnnouncementPublicationProcessTest.java @@ -30,7 +30,6 @@ import org.mockito.MockitoAnnotations; import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.*; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; /** diff --git a/pom.xml b/pom.xml index 4618a2fc..6b436da6 100644 --- a/pom.xml +++ b/pom.xml @@ -34,8 +34,7 @@ UTF-8 - 3.1.0 - 2.0.4 + 4.3.1 4.13.1 1.4.200 @@ -83,20 +82,14 @@ - org.powermock - powermock-module-junit4 - ${version.powermock} - test - - - org.powermock - powermock-api-mockito2 - ${version.powermock} + org.mockito + mockito-core + ${version.mockito} test org.mockito - mockito-core + mockito-inline ${version.mockito} test @@ -138,6 +131,11 @@ mockito-core test + + org.mockito + mockito-inline + test + org.slf4j slf4j-simple @@ -153,16 +151,6 @@ java-uuid-generator test - - org.powermock - powermock-module-junit4 - test - - - org.powermock - powermock-api-mockito2 - test -