From 43a2784387b103b9eff5f90c288cdf0716fcb034 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Mon, 1 Jun 2015 07:59:10 +0000 Subject: [PATCH] Use mock in unit test for ClearanceLevelManager. Although I'm not sure exactly why this unit test failed on the Sonar build, it seems sensible to remove the dependency on another class by using a mock. +review RM git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@105192 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../classification/ClassificationServiceBootstrap.java | 2 ++ .../ClassificationServiceBootstrapUnitTest.java | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java index 3e4363ac2e..42c3782823 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java @@ -71,6 +71,8 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem /** Set the object from which configuration options will be read. */ public void setClassificationServiceDAO(ClassificationServiceDAO classificationServiceDAO) { this.classificationServiceDAO = classificationServiceDAO; } public void setAttributeService(AttributeService attributeService) { this.attributeService = attributeService; } + /** Used in unit tests. */ + protected void setClearanceLevelManager(ClearanceLevelManager clearanceLevelManager) { this.clearanceLevelManager = clearanceLevelManager; } public ClassificationLevelManager getClassificationLevelManager() { return classificationLevelManager; } public ClassificationReasonManager getClassificationReasonManager() { return classificationReasonManager; } diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java index 0ae13faf8d..b327017f36 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrapUnitTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -98,14 +99,18 @@ public class ClassificationServiceBootstrapUnitTest @Mock ClassificationServiceDAO mockClassificationServiceDAO; @Mock AttributeService mockAttributeService; @Mock AuthenticationUtil mockAuthenticationUtil; + @Mock ClearanceLevelManager mockClearanceLevelManager; /** Using a mock appender in the class logger so that we can verify some of the logging requirements. */ @Mock Appender mockAppender; @Captor ArgumentCaptor loggingEventCaptor; + @Captor ArgumentCaptor> clearanceLevelCaptor; @Before public void setUp() { MockitoAnnotations.initMocks(this); MockAuthenticationUtilHelper.setup(mockAuthenticationUtil); + // This seems to be necessary because the ClearanceLevelManager isn't a constructor argument. + classificationServiceBootstrap.setClearanceLevelManager(mockClearanceLevelManager); } @Test public void defaultLevelsConfigurationVanillaSystem() @@ -223,11 +228,12 @@ public class ClassificationServiceBootstrapUnitTest ClassificationLevel topSecret = new ClassificationLevel("1", "TopSecret"); ClassificationLevel secret = new ClassificationLevel("2", "Secret"); ImmutableList classificationLevels = ImmutableList.of(topSecret, secret, ClassificationLevelManager.UNCLASSIFIED); + doNothing().when(mockClearanceLevelManager).setClearanceLevels(clearanceLevelCaptor.capture()); // Call the method under test. classificationServiceBootstrap.initConfiguredClearanceLevels(classificationLevels); - List clearanceLevels = classificationServiceBootstrap.getClearanceLevelManager().getClearanceLevels(); + List clearanceLevels = clearanceLevelCaptor.getValue(); assertEquals("There should be one clearance level for each classification level.", classificationLevels.size(), clearanceLevels.size()); assertEquals("TopSecret", clearanceLevels.get(0).getDisplayLabel()); assertEquals("Secret", clearanceLevels.get(1).getDisplayLabel());