Author: sseifert Date: Tue Dec 20 20:54:41 2016 New Revision: 1775353 URL: http://svn.apache.org/viewvc?rev=1775353&view=rev Log: replace jmock with mockito
Modified: sling/trunk/bundles/api/pom.xml sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/NonExistingResourceTest.java sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java Modified: sling/trunk/bundles/api/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/pom.xml?rev=1775353&r1=1775352&r2=1775353&view=diff ============================================================================== --- sling/trunk/bundles/api/pom.xml (original) +++ sling/trunk/bundles/api/pom.xml Tue Dec 20 20:54:41 2016 @@ -74,10 +74,6 @@ <artifactId>junit</artifactId> </dependency> <dependency> - <groupId>org.jmock</groupId> - <artifactId>jmock-junit4</artifactId> - </dependency> - <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.commons.testing</artifactId> <version>2.1.0</version> Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/NonExistingResourceTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/NonExistingResourceTest.java?rev=1775353&r1=1775352&r2=1775353&view=diff ============================================================================== --- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/NonExistingResourceTest.java (original) +++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/NonExistingResourceTest.java Tue Dec 20 20:54:41 2016 @@ -18,33 +18,26 @@ */ package org.apache.sling.api.resource; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JMock; -import org.jmock.integration.junit4.JUnit4Mockery; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(JMock.class) +@RunWith(MockitoJUnitRunner.class) public class NonExistingResourceTest { - protected final Mockery context = new JUnit4Mockery(); - protected ResourceResolver resolver; - - @Before - public void setUp() { - resolver = this.context.mock(ResourceResolver.class); - } + @Mock + private ResourceResolver resolver; @Test public void testGetParentWithNonExistingParent() { final NonExistingResource nonExistingResource = new NonExistingResource(resolver, "/nonExistingParent/nonExistingResource"); - context.checking(new Expectations() {{ - allowing(resolver).getParent(nonExistingResource); will(returnValue(null)); - }}); + when(resolver.getParent(nonExistingResource)).thenReturn(null); Resource parentResource = nonExistingResource.getParent(); Assert.assertNotNull("Non existing parent of NonExistingResource must not return null!", parentResource); @@ -56,16 +49,15 @@ public class NonExistingResourceTest { public void testGetParentWithExistingParent() throws PersistenceException { final NonExistingResource nonExistingResource = new NonExistingResource(resolver, "/existingParent/nonExistingResource"); - final Resource mockParentResource = this.context.mock(Resource.class); - context.checking(new Expectations() {{ - allowing(resolver).getParent(nonExistingResource); will(returnValue(mockParentResource)); - allowing(mockParentResource).getPath(); will(returnValue("/existingParent")); - allowing(mockParentResource).getResourceType(); will(returnValue("anyResourceType")); - }}); + final Resource mockParentResource = mock(Resource.class); + when(resolver.getParent(nonExistingResource)).thenReturn(mockParentResource); + when(mockParentResource.getPath()).thenReturn("/existingParent"); + when(mockParentResource.getResourceType()).thenReturn("anyResourceType"); Resource parentResource = nonExistingResource.getParent(); Assert.assertNotNull("Existing parent of NonExistingResource must not return null!", parentResource); Assert.assertEquals("/existingParent", parentResource.getPath()); Assert.assertFalse(ResourceUtil.isNonExistingResource(parentResource)); } + } Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java?rev=1775353&r1=1775352&r2=1775353&view=diff ============================================================================== --- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java (original) +++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java Tue Dec 20 20:54:41 2016 @@ -24,6 +24,8 @@ import static org.junit.Assert.assertNot import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.HashMap; @@ -33,18 +35,13 @@ import java.util.Map; import java.util.NoSuchElementException; import org.apache.sling.api.wrappers.ValueMapDecorator; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JMock; -import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(JMock.class) +@RunWith(MockitoJUnitRunner.Silent.class) public class ResourceUtilTest { - protected final Mockery context = new JUnit4Mockery(); - @Test public void testResolveRelativeSegments() { assertEquals("/", ResourceUtil.normalize("/")); @@ -265,28 +262,27 @@ public class ResourceUtilTest { // none is adaptable to String // b and c are adaptable to long // a and c are adaptable to boolean - final Resource a = this.context.mock(Resource.class, "a"); - final Resource b = this.context.mock(Resource.class, "b"); - final Resource c = this.context.mock(Resource.class, "c"); + final Resource a = mock(Resource.class, "a"); + final Resource b = mock(Resource.class, "b"); + final Resource c = mock(Resource.class, "c"); final List<Resource> l = new ArrayList<Resource>(); l.add(a); l.add(b); l.add(c); - this.context.checking(new Expectations() {{ - allowing(a).adaptTo(List.class); will(returnValue(new ArrayList<Object>())); - allowing(b).adaptTo(List.class); will(returnValue(new ArrayList<Object>())); - allowing(c).adaptTo(List.class); will(returnValue(null)); - allowing(a).adaptTo(Map.class); will(returnValue(new HashMap<Object, Object>())); - allowing(b).adaptTo(Map.class); will(returnValue(new HashMap<Object, Object>())); - allowing(c).adaptTo(Map.class); will(returnValue(new HashMap<Object, Object>())); - allowing(a).adaptTo(Long.class); will(returnValue(null)); - allowing(b).adaptTo(Long.class); will(returnValue(new Long(1))); - allowing(c).adaptTo(Long.class); will(returnValue(new Long(2))); - allowing(a).adaptTo(Boolean.class); will(returnValue(new Boolean(true))); - allowing(b).adaptTo(Boolean.class); will(returnValue(null)); - allowing(c).adaptTo(Boolean.class); will(returnValue(new Boolean(false))); - allowing(a).adaptTo(String.class); will(returnValue(null)); - allowing(b).adaptTo(String.class); will(returnValue(null)); - allowing(c).adaptTo(String.class); will(returnValue(null)); - }}); + + when(a.adaptTo(List.class)).thenReturn(new ArrayList<Object>()); + when(b.adaptTo(List.class)).thenReturn(new ArrayList<Object>()); + when(c.adaptTo(List.class)).thenReturn(null); + when(a.adaptTo(Map.class)).thenReturn(new HashMap<Object, Object>()); + when(b.adaptTo(Map.class)).thenReturn(new HashMap<Object, Object>()); + when(c.adaptTo(Map.class)).thenReturn(new HashMap<Object, Object>()); + when(a.adaptTo(Long.class)).thenReturn(null); + when(b.adaptTo(Long.class)).thenReturn(new Long(1)); + when(c.adaptTo(Long.class)).thenReturn(new Long(2)); + when(a.adaptTo(Boolean.class)).thenReturn(new Boolean(true)); + when(b.adaptTo(Boolean.class)).thenReturn(null); + when(c.adaptTo(Boolean.class)).thenReturn(new Boolean(false)); + when(a.adaptTo(String.class)).thenReturn(null); + when(b.adaptTo(String.class)).thenReturn(null); + when(c.adaptTo(String.class)).thenReturn(null); assertEquals(2, checkIterator(l, List.class)); assertEquals(3, checkIterator(l, Map.class)); @@ -318,14 +314,13 @@ public class ResourceUtilTest { } @Test public void testIsStarResource() { - final Resource nonStar = context.mock(Resource.class, "nonStarResource"); + final Resource nonStar = mock(Resource.class, "nonStarResource"); final String starPath = "/foo/*"; - final Resource star = context.mock(Resource.class, "starResource"); + final Resource star = mock(Resource.class, "starResource"); final String nonStarPath = "/foo/*"; - this.context.checking(new Expectations() {{ - allowing(star).getPath(); will(returnValue(starPath)); - allowing(nonStar).getPath(); will(returnValue(nonStarPath)); - }}); + + when(star.getPath()).thenReturn(starPath); + when(nonStar.getPath()).thenReturn(nonStarPath); assertTrue("expecting star==true for path" + starPath, ResourceUtil.isStarResource(star)); @@ -334,10 +329,10 @@ public class ResourceUtilTest { } @Test public void testIsSyntheticResource() { final Resource synth = new SyntheticResource(null, "foo", "bar"); - final Resource star = context.mock(Resource.class); - this.context.checking(new Expectations() {{ - allowing(star).getPath(); will(returnValue("/foo/*")); - }}); + final Resource star = mock(Resource.class); + + when(star.getPath()).thenReturn("/foo/*"); + final Resource wrapped = new ResourceWrapper(synth); assertTrue("expecting synthetic==true for SyntheticResource",