Commit 4c27f3a9 authored by cijujoseph's avatar cijujoseph

Adding a few examples for java class testing

parent 32ad9148
......@@ -3,19 +3,20 @@ package com.alfresco.aps.example.listeners;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;
import org.joda.time.DateTime;
@Component("processEndExecutionListener")
public class ProcessEndExecutionListener implements ExecutionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void notify(DelegateExecution execution) throws Exception {
// TODO Auto-generated method stub
System.out.println(execution.getId());
if(DateTime.now().getDayOfMonth() % 2 == 0){
execution.setVariable("oddOrEven", "EVEN");
} else {
execution.setVariable("oddOrEven", "ODD");
}
}
}
......@@ -2,6 +2,7 @@ package com.alfresco.aps.example.listeners;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
@Component("taskAssignedTaskListener")
......@@ -14,7 +15,11 @@ public class TaskAssignedTaskListener implements TaskListener {
@Override
public void notify(DelegateTask task) {
System.out.println(task.getId());
if(DateTime.now().getDayOfMonth() % 2 == 0){
task.setVariable("oddOrEven", "EVENDATE");
} else {
task.setVariable("oddOrEven", "ODDDATE");
}
}
}
package com.alfresco.aps.example.listeners;
import java.util.HashMap;
import java.util.Map;
import org.activiti.engine.delegate.DelegateExecution;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ProcessEndExecutionListenerTest {
@Configuration
static class ContextConfiguration {
@Bean
public ProcessEndExecutionListener processEndExecutionListener() {
return new ProcessEndExecutionListener();
}
}
@InjectMocks
@Spy
private static ProcessEndExecutionListener processEndExecutionListener;
@Mock
private DelegateExecution execution;
@Before
public void initMocks(){
MockitoAnnotations.initMocks(this);
}
@Test
public void test() throws Exception {
Map<String, Object> variableMap = new HashMap<String, Object>();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arg = invocation.getArguments();
variableMap.put((String) arg[0], arg[1]);
return null;
}
}).when(execution).setVariable(anyString(), any());;
when(execution.getVariable(anyString())).thenAnswer(new Answer<String>() {
public String answer(InvocationOnMock invocation) {
return (String) variableMap.get(invocation.getArguments()[0]);
}
});
processEndExecutionListener.notify(execution);
//sample assertion to make sure that the java code is setting correct value
assertThat(execution.getVariable("oddOrEven")).isNotNull().isIn("ODD", "EVEN");
}
}
package com.alfresco.aps.example.listeners;
import java.util.HashMap;
import java.util.Map;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TaskAssignedTaskListenerTest {
@Configuration
static class ContextConfiguration {
@Bean
public TaskAssignedTaskListener taskAssignedTaskListener() {
return new TaskAssignedTaskListener();
}
}
@InjectMocks
@Spy
private static TaskAssignedTaskListener taskAssignedTaskListener;
@Mock
private DelegateTask task;
@Before
public void initMocks(){
MockitoAnnotations.initMocks(this);
}
@Test
public void test() throws Exception {
Map<String, Object> variableMap = new HashMap<String, Object>();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arg = invocation.getArguments();
variableMap.put((String) arg[0], arg[1]);
return null;
}
}).when(task).setVariable(anyString(), any());;
when(task.getVariable(anyString())).thenAnswer(new Answer<String>() {
public String answer(InvocationOnMock invocation) {
return (String) variableMap.get(invocation.getArguments()[0]);
}
});
taskAssignedTaskListener.notify(task);
//sample assertion to make sure that the java code is setting correct value
assertThat(task.getVariable("oddOrEven")).isNotNull().isIn("ODDDATE", "EVENDATE");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment