camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/3] camel git commit: CAMEL-11845: Migrate easymock and powermock to mockito
Date Tue, 03 Oct 2017 07:29:54 GMT
CAMEL-11845: Migrate easymock and powermock to mockito

Migrate remaining camel-core easymock tests to mockito.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d8f8b125
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d8f8b125
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d8f8b125

Branch: refs/heads/master
Commit: d8f8b125872fa309f52b56251a1b6c6b2ac188cf
Parents: e2aee2d
Author: Pascal Schumacher <pascalschumacher@gmx.net>
Authored: Fri Sep 29 21:02:16 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Tue Oct 3 09:27:36 2017 +0200

----------------------------------------------------------------------
 camel-core/pom.xml                              |  6 --
 .../rest/RestProducerBindingProcessorTest.java  | 62 +++++++-------------
 .../camel/impl/DefaultFactoryFinderTest.java    | 29 ++++-----
 .../DefaultManagementAgentMockTest.java         | 28 ++++-----
 4 files changed, 43 insertions(+), 82 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d8f8b125/camel-core/pom.xml
----------------------------------------------------------------------
diff --git a/camel-core/pom.xml b/camel-core/pom.xml
index ab1f62f..4138db0 100644
--- a/camel-core/pom.xml
+++ b/camel-core/pom.xml
@@ -191,12 +191,6 @@
     </dependency>
 
     <dependency>
-      <groupId>org.easymock</groupId>
-      <artifactId>easymock</artifactId>
-      <scope>test</scope>
-    </dependency>
-
-    <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
       <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/camel/blob/d8f8b125/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerBindingProcessorTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerBindingProcessorTest.java
b/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerBindingProcessorTest.java
index 0ee264e..fc7f12d 100644
--- a/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerBindingProcessorTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerBindingProcessorTest.java
@@ -28,20 +28,16 @@ import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.DefaultExchange;
 import org.apache.camel.impl.DefaultMessage;
 import org.apache.camel.spi.DataFormat;
-import org.easymock.Capture;
-import org.easymock.EasyMock;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
 
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.mock;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.same;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.same;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class RestProducerBindingProcessorTest {
 
@@ -74,28 +70,23 @@ public class RestProducerBindingProcessorTest {
         input.setBody(request);
         exchange.setIn(input);
 
-        jsonDataFormat.marshal(same(exchange), same(request), anyObject(OutputStream.class));
-        expectLastCall().andVoid();
-
         final ResponsePojo response = new ResponsePojo();
-        expect(outJsonDataFormat.unmarshal(same(exchange), anyObject(InputStream.class))).andReturn(response);
-
-        final Capture<AsyncCallback> bindingCallback = EasyMock.newCapture();
+        when(outJsonDataFormat.unmarshal(same(exchange), any(InputStream.class))).thenReturn(response);
 
-        expect(processor.process(same(exchange), capture(bindingCallback))).andReturn(false);
+        final ArgumentCaptor<AsyncCallback> bindingCallback = ArgumentCaptor.forClass(AsyncCallback.class);
 
-        replay(jsonDataFormat, outJsonDataFormat, processor);
+        when(processor.process(same(exchange), bindingCallback.capture())).thenReturn(false);
 
         bindingProcessor.process(exchange, callback);
 
-        assertTrue(bindingCallback.hasCaptured());
+        verify(jsonDataFormat).marshal(same(exchange), same(request), any(OutputStream.class));
+
+        assertNotNull(bindingCallback.getValue());
 
         final AsyncCallback that = bindingCallback.getValue();
 
         that.done(false);
 
-        verify(jsonDataFormat, outJsonDataFormat, processor);
-
         Assert.assertSame(response, exchange.getOut().getBody());
     }
 
@@ -113,28 +104,23 @@ public class RestProducerBindingProcessorTest {
         input.setBody(request);
         exchange.setIn(input);
 
-        xmlDataFormat.marshal(same(exchange), same(request), anyObject(OutputStream.class));
-        expectLastCall().andVoid();
-
         final ResponsePojo response = new ResponsePojo();
-        expect(outXmlDataFormat.unmarshal(same(exchange), anyObject(InputStream.class))).andReturn(response);
+        when(outXmlDataFormat.unmarshal(same(exchange), any(InputStream.class))).thenReturn(response);
 
-        final Capture<AsyncCallback> bindingCallback = EasyMock.newCapture();
+        final ArgumentCaptor<AsyncCallback> bindingCallback = ArgumentCaptor.forClass(AsyncCallback.class);
 
-        expect(processor.process(same(exchange), capture(bindingCallback))).andReturn(false);
-
-        replay(xmlDataFormat, outXmlDataFormat, processor);
+        when(processor.process(same(exchange), bindingCallback.capture())).thenReturn(false);
 
         bindingProcessor.process(exchange, callback);
 
-        assertTrue(bindingCallback.hasCaptured());
+        verify(xmlDataFormat).marshal(same(exchange), same(request), any(OutputStream.class));
+
+        assertNotNull(bindingCallback.getValue());
 
         final AsyncCallback that = bindingCallback.getValue();
 
         that.done(false);
 
-        verify(xmlDataFormat, outXmlDataFormat, processor);
-
         Assert.assertSame(response, exchange.getOut().getBody());
     }
 
@@ -152,20 +138,16 @@ public class RestProducerBindingProcessorTest {
         input.setBody(request);
         exchange.setIn(input);
 
-        final Capture<AsyncCallback> bindingCallback = EasyMock.newCapture();
+        final ArgumentCaptor<AsyncCallback> bindingCallback = ArgumentCaptor.forClass(AsyncCallback.class);
 
-        expect(processor.process(same(exchange), capture(bindingCallback))).andReturn(false);
-
-        replay(processor);
+        when(processor.process(same(exchange), bindingCallback.capture())).thenReturn(false);
 
         bindingProcessor.process(exchange, callback);
 
-        assertTrue(bindingCallback.hasCaptured());
+        assertNotNull(bindingCallback.getValue());
 
         final AsyncCallback that = bindingCallback.getValue();
 
         that.done(false);
-
-        verify(processor);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/d8f8b125/camel-core/src/test/java/org/apache/camel/impl/DefaultFactoryFinderTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/DefaultFactoryFinderTest.java
b/camel-core/src/test/java/org/apache/camel/impl/DefaultFactoryFinderTest.java
index 2d36c77..e3fb507 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/DefaultFactoryFinderTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/DefaultFactoryFinderTest.java
@@ -26,11 +26,6 @@ import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.spi.Injector;
 import org.junit.Test;
 
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.mock;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
 import static org.hamcrest.Matchers.matchesPattern;
 import static org.hamcrest.core.IsCollectionContaining.hasItem;
 import static org.hamcrest.core.IsInstanceOf.instanceOf;
@@ -38,6 +33,9 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class DefaultFactoryFinderTest {
 
@@ -60,12 +58,10 @@ public class DefaultFactoryFinderTest {
 
         final String properties = "class=" + TestImplA.class.getName();
 
-        expect(classResolver.loadResourceAsStream("/org/apache/camel/impl/TestImplA"))
-                .andReturn(new ByteArrayInputStream(properties.getBytes()));
+        when(classResolver.loadResourceAsStream("/org/apache/camel/impl/TestImplA"))
+                .thenReturn(new ByteArrayInputStream(properties.getBytes()));
 
-        expect(classResolver.resolveClass(TestImplA.class.getName())).andReturn(null);
-
-        replay(classResolver);
+        when(classResolver.resolveClass(TestImplA.class.getName())).thenReturn(null);
 
         final DefaultFactoryFinder factoryFinder = new DefaultFactoryFinder(classResolver,
TEST_RESOURCE_PATH);
 
@@ -73,7 +69,6 @@ public class DefaultFactoryFinderTest {
             factoryFinder.findClass("TestImplA", null);
             fail("Should have thrown ClassNotFoundException");
         } catch (final ClassNotFoundException e) {
-            verify(classResolver);
             assertEquals(TestImplA.class.getName(), e.getMessage());
         }
 
@@ -81,12 +76,10 @@ public class DefaultFactoryFinderTest {
 
     @Test
     public void shouldComplainIfInstanceTypeIsNotAsExpected() throws ClassNotFoundException,
IOException {
-        final Injector injector = createMock(Injector.class);
+        final Injector injector = mock(Injector.class);
 
         final TestImplA expected = new TestImplA();
-        expect(injector.newInstance(TestImplA.class)).andReturn(expected);
-
-        replay(injector);
+        when(injector.newInstance(TestImplA.class)).thenReturn(expected);
 
         try {
             factoryFinder.newInstances("TestImplA", injector, TestImplB.class);
@@ -128,12 +121,10 @@ public class DefaultFactoryFinderTest {
 
     @Test
     public void shouldCreateNewInstancesWithInjector() throws ClassNotFoundException, IOException
{
-        final Injector injector = createMock(Injector.class);
+        final Injector injector = mock(Injector.class);
 
         final TestImplA expected = new TestImplA();
-        expect(injector.newInstance(TestImplA.class)).andReturn(expected);
-
-        replay(injector);
+        when(injector.newInstance(TestImplA.class)).thenReturn(expected);
 
         final List<TestType> instances = factoryFinder.newInstances("TestImplA", injector,
TestType.class);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/d8f8b125/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
b/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
index bd469a1..5066a63 100644
--- a/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
+++ b/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
@@ -26,13 +26,11 @@ import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.spi.ManagementAgent;
 import org.junit.Test;
 
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.when;
 
 /**
  * Tests proper behavior of DefaultManagementAgent when
@@ -43,8 +41,8 @@ public class DefaultManagementAgentMockTest {
 
     @Test
     public void testObjectNameModification() throws JMException {
-        MBeanServer mbeanServer = createStrictMock(MBeanServer.class);
-        ObjectInstance instance = createStrictMock(ObjectInstance.class);
+        MBeanServer mbeanServer = mock(MBeanServer.class);
+        ObjectInstance instance = mock(ObjectInstance.class);
 
         ManagementAgent agent = new DefaultManagementAgent();
         agent.setMBeanServer(mbeanServer);
@@ -54,28 +52,24 @@ public class DefaultManagementAgentMockTest {
         ObjectName registeredObjectName = new ObjectName("domain", "key", "otherValue");
 
         // Register MBean and return different ObjectName
-        expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
-        expect(mbeanServer.registerMBean(object, sourceObjectName)).andReturn(instance);
-        expect(instance.getObjectName()).andReturn(registeredObjectName);
-        expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
-        replay(mbeanServer, instance);
+        when(mbeanServer.isRegistered(sourceObjectName)).thenReturn(false);
+        when(mbeanServer.registerMBean(object, sourceObjectName)).thenReturn(instance);
+        when(instance.getObjectName()).thenReturn(registeredObjectName);
+        when(mbeanServer.isRegistered(registeredObjectName)).thenReturn(true);
 
         agent.register(object, sourceObjectName);
 
         assertTrue(agent.isRegistered(sourceObjectName));
-        verify(mbeanServer, instance);
         reset(mbeanServer, instance);
 
         // ... and unregister it again
-        expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
+        when(mbeanServer.isRegistered(registeredObjectName)).thenReturn(true);
         mbeanServer.unregisterMBean(registeredObjectName);
-        expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
-        replay(mbeanServer);
+        when(mbeanServer.isRegistered(sourceObjectName)).thenReturn(false);
 
         agent.unregister(sourceObjectName);
 
         assertFalse(agent.isRegistered(sourceObjectName));
-        verify(mbeanServer);
     }
 
     @Test


Mime
View raw message