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

Commit

Permalink
[tests] Fixing the unit tests for the late version of Mockito.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 26, 2014
1 parent 2afd75f commit b421f7b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
package io.janusproject.kernel;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.janusproject.services.IServiceManager;
import io.janusproject.services.contextspace.ContextSpaceService;
import io.janusproject.services.executor.ExecutorService;
Expand All @@ -39,13 +43,11 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Matchers;
import org.mockito.Mockito;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.util.concurrent.Service;
import com.google.common.util.concurrent.Service.State;


/**
* @author $Author: sgalland$
* @version $FullVersion$
Expand All @@ -68,28 +70,29 @@ public class KernelTest extends Assert {
@Before
public void setUp() {
this.uuid = UUID.randomUUID();
this.spawnService = Mockito.mock(SpawnService.class);
this.executorService = Mockito.mock(ExecutorService.class);
this.contextService = Mockito.mock(ContextSpaceService.class);
this.spawnService = mock(SpawnService.class);
this.executorService = mock(ExecutorService.class);
this.contextService = mock(ContextSpaceService.class);
this.services = ImmutableMultimap.<State,Service>of(
State.RUNNING, this.spawnService,
State.RUNNING, this.executorService,
State.RUNNING, this.contextService);
this.agentContext = Mockito.mock(AgentContext.class);
this.exceptionHandler = Mockito.mock(UncaughtExceptionHandler.class);
this.serviceManager = Mockito.mock(IServiceManager.class);
this.agentContext = mock(AgentContext.class);
this.exceptionHandler = mock(UncaughtExceptionHandler.class);
this.serviceManager = mock(IServiceManager.class);
//
Mockito.when(this.spawnService.isRunning()).thenReturn(true);
Mockito.when(this.spawnService.state()).thenReturn(State.RUNNING);
Mockito.when(this.spawnService.spawn(Matchers.any(AgentContext.class), Matchers.any(UUID.class), Matchers.any(Class.class), Matchers.anyString(), Matchers.anyString())).thenReturn(this.uuid);
Mockito.when(this.executorService.isRunning()).thenReturn(true);
Mockito.when(this.executorService.state()).thenReturn(State.RUNNING);
Mockito.when(this.contextService.isRunning()).thenReturn(true);
Mockito.when(this.contextService.state()).thenReturn(State.RUNNING);
Mockito.when(this.serviceManager.servicesByState()).thenReturn(this.services);
when(this.spawnService.isRunning()).thenReturn(true);
when(this.spawnService.state()).thenReturn(State.RUNNING);
when(this.spawnService.spawn(Matchers.any(AgentContext.class), Matchers.any(UUID.class), Matchers.any(Class.class),
Matchers.anyVararg())).thenReturn(this.uuid);
when(this.executorService.isRunning()).thenReturn(true);
when(this.executorService.state()).thenReturn(State.RUNNING);
when(this.contextService.isRunning()).thenReturn(true);
when(this.contextService.state()).thenReturn(State.RUNNING);
when(this.serviceManager.servicesByState()).thenReturn(this.services);
//
this.kernel = new Kernel(this.serviceManager, this.spawnService, this.exceptionHandler);
this.kernel = Mockito.spy(this.kernel);
this.kernel = spy(this.kernel);
}

@After
Expand Down Expand Up @@ -122,16 +125,13 @@ public void spawn() {
ArgumentCaptor<UUID> argument2 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Class> argument3 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<String> argument4 = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> argument5 = ArgumentCaptor.forClass(String.class);
Mockito.verify(this.spawnService).spawn(
argument1.capture(), argument2.capture(),
argument3.capture(), argument4.capture(),
argument5.capture());
verify(this.spawnService).spawn(
argument1.capture(), argument2.capture(), argument3.capture(),
argument4.capture());
assertSame(this.agentContext, argument1.getValue());
assertNull(argument2.getValue());
assertEquals(Agent.class, argument3.getValue());
assertEquals("a", argument4.getValue()); //$NON-NLS-1$
assertEquals("b", argument5.getValue()); //$NON-NLS-1$
assertArrayEquals(new String[] { "a", "b" }, argument4.getAllValues().toArray()); //$NON-NLS-1$//$NON-NLS-2$
}

@Test
Expand All @@ -146,16 +146,13 @@ public void spawnWithAgentId() {
ArgumentCaptor<UUID> argument2 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Class> argument3 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<String> argument4 = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> argument5 = ArgumentCaptor.forClass(String.class);
Mockito.verify(this.spawnService).spawn(
verify(this.spawnService).spawn(
argument1.capture(), argument2.capture(),
argument3.capture(), argument4.capture(),
argument5.capture());
argument3.capture(), argument4.capture());
assertSame(this.agentContext, argument1.getValue());
assertSame(aId, argument2.getValue());
assertEquals(Agent.class, argument3.getValue());
assertEquals("a", argument4.getValue()); //$NON-NLS-1$
assertEquals("b", argument5.getValue()); //$NON-NLS-1$
assertArrayEquals(new String[] { "a", "b" }, argument4.getAllValues().toArray()); //$NON-NLS-1$//$NON-NLS-2$
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
package io.janusproject.kernel.bic;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.sarl.core.Lifecycle;
import io.sarl.lang.core.Address;
import io.sarl.lang.core.Agent;
Expand All @@ -39,7 +43,6 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.mockito.internal.verification.Times;

/**
Expand All @@ -66,13 +69,13 @@ public void setUp() throws Exception {
EventSpaceSpecification.class),
UUID.randomUUID());

this.defaultSpace = Mockito.mock(EventSpace.class);
Mockito.when(this.defaultSpace.getAddress(Matchers.any(UUID.class))).thenReturn(this.address);
this.defaultSpace = mock(EventSpace.class);
when(this.defaultSpace.getAddress(Matchers.any(UUID.class))).thenReturn(this.address);

this.parentContext = Mockito.mock(AgentContext.class);
Mockito.when(this.parentContext.getDefaultSpace()).thenReturn(this.defaultSpace);
this.parentContext = mock(AgentContext.class);
when(this.parentContext.getDefaultSpace()).thenReturn(this.defaultSpace);

this.lifeCapacity = Mockito.mock(Lifecycle.class);
this.lifeCapacity = mock(Lifecycle.class);

Agent agent = new Agent(UUID.randomUUID()) {
@Override
Expand Down Expand Up @@ -120,47 +123,44 @@ public void install() {
@Test
public void emitEventScope() {
this.skill.install();
Event event = Mockito.mock(Event.class);
Event event = mock(Event.class);
Scope<Address> scope = Scopes.allParticipants();
this.skill.emit(event, scope);
ArgumentCaptor<Event> argument1 = ArgumentCaptor.forClass(Event.class);
ArgumentCaptor<Scope> argument2 = ArgumentCaptor.forClass(Scope.class);
Mockito.verify(this.defaultSpace, new Times(1)).emit(argument1.capture(), argument2.capture());
verify(this.defaultSpace, new Times(1)).emit(argument1.capture(), argument2.capture());
assertSame(event, argument1.getValue());
assertSame(scope, argument2.getValue());
ArgumentCaptor<Address> argument3 = ArgumentCaptor.forClass(Address.class);
Mockito.verify(event).setSource(argument3.capture());
verify(event).setSource(argument3.capture());
assertEquals(this.address, argument3.getValue());
}

@Test
public void emitEvent() {
this.skill.install();
Event event = Mockito.mock(Event.class);
Event event = mock(Event.class);
this.skill.emit(event);
ArgumentCaptor<Event> argument1 = ArgumentCaptor.forClass(Event.class);
Mockito.verify(this.defaultSpace, new Times(1)).emit(argument1.capture());
verify(this.defaultSpace, new Times(1)).emit(argument1.capture());
assertSame(event, argument1.getValue());
ArgumentCaptor<Address> argument2 = ArgumentCaptor.forClass(Address.class);
Mockito.verify(event).setSource(argument2.capture());
verify(event).setSource(argument2.capture());
assertEquals(this.address, argument2.getValue());
}

@Test
public void spawn() {
this.skill.install();
this.skill.spawn(Agent.class, "a", "b", "c"); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
this.skill.spawn(Agent.class, "a", "b", "c"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
ArgumentCaptor<Class> argument1 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<AgentContext> argument2 = ArgumentCaptor.forClass(AgentContext.class);
ArgumentCaptor<Object> argument3 = ArgumentCaptor.forClass(Object.class);
ArgumentCaptor<Object> argument4 = ArgumentCaptor.forClass(Object.class);
ArgumentCaptor<Object> argument5 = ArgumentCaptor.forClass(Object.class);
Mockito.verify(this.lifeCapacity, new Times(1)).spawnInContext(argument1.capture(), argument2.capture(), argument3.capture(), argument4.capture(), argument5.capture());
ArgumentCaptor<String> argument3 = ArgumentCaptor.forClass(String.class);
verify(this.lifeCapacity, times(1)).spawnInContext(argument1.capture(),
argument2.capture(), argument3.capture());
assertEquals(Agent.class, argument1.getValue());
assertSame(this.parentContext, argument2.getValue());
assertEquals("a", argument3.getValue()); //$NON-NLS-1$
assertEquals("b", argument4.getValue()); //$NON-NLS-1$
assertEquals("c", argument5.getValue()); //$NON-NLS-1$
assertArrayEquals(new String[] { "a", "b", "c" }, argument3.getAllValues().toArray()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package io.janusproject.kernel.bic;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import io.janusproject.services.executor.ChuckNorrisException;
import io.janusproject.services.spawn.SpawnService;
import io.sarl.lang.core.Agent;
Expand All @@ -36,9 +39,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.verification.Times;

/**
* @author $Author: srodriguez$
Expand Down Expand Up @@ -90,39 +91,35 @@ public void tearDown() throws Exception {
@Test
public void spawnInContext() {
Class type = Agent.class;
AgentContext context = Mockito.mock(AgentContext.class);
Object[] params = new Object[] {1,"String"}; //$NON-NLS-1$
this.skill.spawnInContext(type, context, params);
AgentContext context = mock(AgentContext.class);
this.skill.spawnInContext(type, context, 1, "String"); //$NON-NLS-1$
ArgumentCaptor<AgentContext> argument1 = ArgumentCaptor.forClass(AgentContext.class);
ArgumentCaptor<UUID> argument2 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Class> argument3 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<Integer> argument4 = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor<String> argument5 = ArgumentCaptor.forClass(String.class);
Mockito.verify(this.spawnService, new Times(1)).spawn(argument1.capture(), argument2.capture(), argument3.capture(), argument4.capture(), argument5.capture());
ArgumentCaptor<Object> argument4 = ArgumentCaptor.forClass(Object.class);
verify(this.spawnService, times(1)).spawn(argument1.capture(), argument2.capture(),
argument3.capture(), argument4.capture());
assertSame(context, argument1.getValue());
assertNull(argument2.getValue());
assertEquals(Agent.class, argument3.getValue());
assertEquals(1, argument4.getValue().intValue());
assertEquals("String", argument5.getValue()); //$NON-NLS-1$
assertArrayEquals(new Object[] { 1, "String" }, argument4.getAllValues().toArray()); //$NON-NLS-1$
}

@Test
public void spawnInContextWithID() {
Class type = Agent.class;
AgentContext context = Mockito.mock(AgentContext.class);
Object[] params = new Object[] {1,"String"}; //$NON-NLS-1$
this.skill.spawnInContextWithID(type, this.agentId, context, params);
AgentContext context = mock(AgentContext.class);
this.skill.spawnInContextWithID(type, this.agentId, context, 1, "String"); //$NON-NLS-1$
ArgumentCaptor<AgentContext> argument1 = ArgumentCaptor.forClass(AgentContext.class);
ArgumentCaptor<UUID> argument2 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Class> argument3 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<Integer> argument4 = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor<String> argument5 = ArgumentCaptor.forClass(String.class);
Mockito.verify(this.spawnService, new Times(1)).spawn(argument1.capture(), argument2.capture(), argument3.capture(), argument4.capture(), argument5.capture());
ArgumentCaptor<Object> argument4 = ArgumentCaptor.forClass(Object.class);
verify(this.spawnService, times(1)).spawn(argument1.capture(), argument2.capture(),
argument3.capture(), argument4.capture());
assertSame(context, argument1.getValue());
assertSame(this.agentId, argument2.getValue());
assertEquals(Agent.class, argument3.getValue());
assertEquals(1, argument4.getValue().intValue());
assertEquals("String", argument5.getValue()); //$NON-NLS-1$
assertArrayEquals(new Object[] { 1, "String" }, argument4.getAllValues().toArray()); //$NON-NLS-1$
}

@Test
Expand All @@ -138,7 +135,7 @@ public void killMe() throws Exception {
throw e;
}
ArgumentCaptor<Event> argument = ArgumentCaptor.forClass(Event.class);
Mockito.verify(this.eventBus, new Times(1)).selfEvent(argument.capture());
verify(this.eventBus, times(1)).selfEvent(argument.capture());
assertThat(argument.getValue(), IsInstanceOf.instanceOf(AsynchronousAgentKillingEvent.class));
}

Expand Down
Loading

0 comments on commit b421f7b

Please sign in to comment.