Skip to content

Commit

Permalink
refactoring mock
Browse files Browse the repository at this point in the history
Issue #823
  • Loading branch information
rsoika committed Aug 29, 2024
1 parent 4eb5bf4 commit 1e2b711
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,6 @@ public void setUp() throws PluginException, ModelException {
workflowEngine.setUp();
workflowEngine.loadBPMNModel("/bpmn/TestWorkflowService.bpmn");

// AdaptText
// when(workflowEngine.workflowService.adaptText(Mockito.anyString(),
// Mockito.any(ItemCollection.class)))
// .thenAnswer(new Answer<String>() {
// @Override
// public String answer(InvocationOnMock invocation) throws Throwable {

// Object[] args = invocation.getArguments();
// String text = (String) args[0];
// ItemCollection document = (ItemCollection) args[1];

// TextEvent textEvent = new TextEvent(text, document);

// // for-each adapter
// TextForEachAdapter tfea = new TextForEachAdapter();
// tfea.onEvent(textEvent);

// // ItemValue adapter
// TextItemValueAdapter tiva = new TextItemValueAdapter();
// tiva.onEvent(textEvent);

// return textEvent.getText();
// }
// });

// when(workflowEngine.workflowService.adaptTextList(Mockito.anyString(),
// Mockito.any(ItemCollection.class)))
// .thenAnswer(new Answer<List<String>>() {
// @Override
// public List<String> answer(InvocationOnMock invocation) throws Throwable,
// PluginException {

// Object[] args = invocation.getArguments();
// String text = (String) args[0];
// ItemCollection document = (ItemCollection) args[1];

// TextEvent textEvent = new TextEvent(text, document);

// TextItemValueAdapter tiva = new TextItemValueAdapter();
// tiva.onEvent(textEvent);

// return textEvent.getTextList();
// }
// });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
Expand All @@ -11,7 +12,6 @@
import org.imixs.workflow.exceptions.ModelException;
import org.imixs.workflow.exceptions.PluginException;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand All @@ -24,6 +24,8 @@
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.util.BPMNModelFactory;

import jakarta.enterprise.event.Event;

/**
* The {@code AbstractWorkflowServiceTest} can be used as a base class for junit
* tests to mock the Imixs WorkflowService. The class mocks the WorkflowService
Expand All @@ -39,7 +41,6 @@
*
* @author rsoika
*/
// @ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class WorkflowEngineMock {
protected final static Logger logger = Logger.getLogger(WorkflowEngineMock.class.getName());
Expand Down Expand Up @@ -83,7 +84,6 @@ public WorkflowService getWorkflowService() {
*
* @throws PluginException
*/
@BeforeEach
public void setUp() throws PluginException {
// Ensures that @Mock and @InjectMocks annotations are processed
MockitoAnnotations.openMocks(this);
Expand All @@ -94,11 +94,12 @@ public void setUp() throws PluginException {

// Link modelService to workflowServiceMock
workflowService.modelService = modelService;
Assert.assertNotNull(modelService.getOpenBPMNModelManager());

workflowContext = new WorkflowContextMock();
workflowService.ctx = workflowContext.getSessionContext();

// Mock Database Service...
// Mock Database Service with a in-memory database...
when(documentService.load(Mockito.anyString())).thenAnswer(new Answer<ItemCollection>() {
@Override
public ItemCollection answer(InvocationOnMock invocation) throws Throwable {
Expand All @@ -124,6 +125,41 @@ public ItemCollection answer(InvocationOnMock invocation) throws Throwable {
}
});

// Mock Event<TextEvent>
Event<TextEvent> mockTextEvents = Mockito.mock(Event.class);

// Set up behavior for the mock to simulate firing adapters
Mockito.doAnswer(invocation -> {
TextEvent event = invocation.getArgument(0);

// Create and use the adapters
TextItemValueAdapter tiva = new TextItemValueAdapter();
TextForEachAdapter tfea = new TextForEachAdapter();

// Invoke adapters
tfea.onEvent(event);
tiva.onEvent(event);

return null;
}).when(mockTextEvents).fire(Mockito.any(TextEvent.class));

// Inject the mocked Event<TextEvent> into the workflowService
injectMockIntoField(workflowService, "textEvents", mockTextEvents);
}

/**
* Helper method that loads a new model into the ModelService
*
* @param modelPath
*/
public void loadBPMNModel(String modelPath) {
try {
BPMNModel model = BPMNModelFactory.read(modelPath);
modelService.getOpenBPMNModelManager().addModel(model);
} catch (BPMNModelException | ModelException e) {
e.printStackTrace();
Assert.fail();
}
}

/**
Expand All @@ -149,17 +185,20 @@ protected void createTestDatabase() {
}

/**
* Helper method that loads a new model into the ModelService
*
* @param modelPath
* Helper method to inject a mock into a private/protected field using
* reflection.
*
* @param targetObject The object into which the field is to be injected.
* @param fieldName The name of the field to inject.
* @param value The mock or object to inject into the field.
*/
public void loadBPMNModel(String modelPath) {
private void injectMockIntoField(Object targetObject, String fieldName, Object value) {
try {
BPMNModel model = BPMNModelFactory.read(modelPath);
modelService.getOpenBPMNModelManager().addModel(model);
} catch (BPMNModelException | ModelException e) {
e.printStackTrace();
Assert.fail();
Field field = targetObject.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(targetObject, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Failed to inject mock into field: " + fieldName, e);
}
}
}

0 comments on commit 1e2b711

Please sign in to comment.