Created
December 12, 2015 16:49
-
-
Save thomashartm/b1bbdb11966b17ad7398 to your computer and use it in GitHub Desktop.
Example mock verification test snippet ... using Mockito in an AEM related unit test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.thartm.someservice.jobs; | |
import static org.mockito.Matchers.any; | |
import static org.mockito.Mockito.never; | |
import static org.mockito.Mockito.times; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.Mockito.when; | |
import java.util.List; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.event.jobs.Job; | |
import org.apache.sling.testing.mock.sling.ResourceResolverType; | |
import org.apache.sling.testing.mock.sling.junit.SlingContext; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.mockito.Mock; | |
import org.mockito.junit.MockitoJUnit; | |
import org.mockito.junit.MockitoRule; | |
import net.thartm.someservice.rollout.RolloutService; | |
import com.day.cq.wcm.api.Page; | |
import com.google.common.collect.Lists; | |
/** | |
* Tests wether the rollout service has been called | |
* @author [email protected] | |
* @since 12/2015 | |
*/ | |
public class MyJobConsumerTest { | |
@Rule | |
public final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK); | |
@Rule | |
public MockitoRule mockitoRule = MockitoJUnit.rule(); | |
private MyJobConsumer consumer; | |
@Mock | |
private Page master; | |
@Mock | |
private Job job; | |
@Mock | |
private RolloutService rolloutServiceMock; | |
@Before | |
public void setUp() throws Exception { | |
this.consumer = new MyJobConsumer(); | |
this.consumer.rolloutService = rolloutServiceMock; | |
} | |
@Test | |
public void testTriggerLiveCopyProcess() throws Exception { | |
final List<String> expectedLanguages = Lists.newArrayList("de", "en", "it"); | |
prepareJobMock("pageName", StringUtils.join(expectedLanguages, ",")); | |
this.consumer.triggerLiveCopyProcess(context.resourceResolver(), master, job); | |
verify(rolloutServiceMock, times(1)).createSite(context.resourceResolver(), master, "pageName", expectedLanguages, | |
MyJobConsumer.ROLLOUT_CONFIG); | |
} | |
@Test | |
public void testTriggerLiveCopyProcessWithoutLanguages() throws Exception { | |
prepareJobMock("pageName", null); | |
this.consumer.triggerLiveCopyProcess(context.resourceResolver(), master, job); | |
verify(rolloutServiceMock, times(1)).createSite(context.resourceResolver(), master, "pageName", | |
MyJobConsumer.ROLLOUT_CONFIG); | |
} | |
@Test | |
public void testTriggerLiveCopyProcessBrokenLanguages() throws Exception { | |
prepareJobMock("pageName", "deenit"); | |
this.consumer.triggerLiveCopyProcess(context.resourceResolver(), master, job); | |
verify(rolloutServiceMock, times(1)).createSite(context.resourceResolver(), master, "pageName", | |
MyJobConsumer.ROLLOUT_CONFIG); | |
} | |
@Test | |
public void testFailToTriggerLiveCopyProcess() throws Exception { | |
prepareJobMock("", "whatever"); | |
this.consumer.triggerLiveCopyProcess(context.resourceResolver(), master, job); | |
verify(rolloutServiceMock, never()).createSite(any(ResourceResolver.class), any(Page.class), any(String.class), any(List.class), | |
any(String.class)); | |
verify(rolloutServiceMock, never()).createSite(any(ResourceResolver.class), any(Page.class), any(String.class), | |
any(String.class)); | |
} | |
public void prepareJobMock(final String pageName, final String languageProperty) { | |
when(job.getProperty(MyJobConsumer.PROPERTY_MPC_NAME)).thenReturn(pageName); | |
when(job.getProperty(MyJobConsumer.PROPERTY_LANGUAGES)).thenReturn(languageProperty); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment