RM-2027 Use annotations to create mocks in unit tests.

+review RM-9

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@100783 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-03-31 10:09:31 +00:00
parent c93dba7c42
commit 420cecd0c7
2 changed files with 13 additions and 54 deletions

View File

@@ -23,9 +23,7 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -44,6 +42,9 @@ import org.apache.log4j.spi.LoggingEvent;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/** /**
* Unit tests for {@link ClassificationServiceImpl}. * Unit tests for {@link ClassificationServiceImpl}.
@@ -90,23 +91,18 @@ public class ClassificationServiceImplUnitTest
return levels; return levels;
} }
private ClassificationServiceImpl classificationServiceImpl; @InjectMocks private ClassificationServiceImpl classificationServiceImpl;
private AttributeService mockedAttributeService = mock(AttributeService.class); @Mock private AttributeService mockedAttributeService;
private AuthenticationUtil mockedAuthenticationUtil; @Mock private AuthenticationUtil mockedAuthenticationUtil;
private ClassificationServiceDAO mockClassificationServiceDAO = mock(ClassificationServiceDAO.class); @Mock private ClassificationServiceDAO mockClassificationServiceDAO;
/** Using a mock appender in the class logger so that we can verify some of the logging requirements. */ /** Using a mock appender in the class logger so that we can verify some of the logging requirements. */
private Appender mockAppender = mock(Appender.class); @Mock private Appender mockAppender;
@Before public void setUp() @Before public void setUp()
{ {
reset(mockClassificationServiceDAO, mockedAttributeService, mockAppender); MockitoAnnotations.initMocks(this);
mockedAuthenticationUtil = MockAuthenticationUtilHelper.create(); MockAuthenticationUtilHelper.setup(mockedAuthenticationUtil);
classificationServiceImpl = new ClassificationServiceImpl();
classificationServiceImpl.setAttributeService(mockedAttributeService);
classificationServiceImpl.setClassificationServiceDAO(mockClassificationServiceDAO);
classificationServiceImpl.setAuthenticationUtil(mockedAuthenticationUtil);
} }
@Test public void defaultLevelsConfigurationVanillaSystem() @Test public void defaultLevelsConfigurationVanillaSystem()

View File

@@ -23,7 +23,6 @@ import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset; import static org.mockito.Mockito.reset;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
@@ -32,54 +31,20 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
/** /**
* A helper to create or initialise mock {@link AuthenticationUtil}s. * A helper to initialise a mock {@link AuthenticationUtil}.
* *
* @author tpage * @author tpage
*/ */
public class MockAuthenticationUtilHelper public class MockAuthenticationUtilHelper
{ {
/**
* Create a Mockito mock <code>AuthenticationUtil</code> that executes all methods assuming the user has permission.
* If the mock is asked for details about the user then it assumes the user is "admin".
*
* @return The new mock.
*/
public static AuthenticationUtil create()
{
AuthenticationUtil mockAuthenticationUtil = mock(AuthenticationUtil.class);
setup(mockAuthenticationUtil);
return mockAuthenticationUtil;
}
/**
* Create a Mockito mock <code>AuthenticationUtil</code> that executes all methods assuming the user has permission.
*
* @param adminUserName The name of the default admin user.
* @param fullyAuthenticatedUser The name of the user that last authenticated.
* @param systemUserName The name of the system user.
* @return The new mock.
*/
public static AuthenticationUtil create(String adminUserName, String fullyAuthenticatedUser, String systemUserName)
{
AuthenticationUtil mockAuthenticationUtil = mock(AuthenticationUtil.class);
setup(mockAuthenticationUtil, adminUserName, fullyAuthenticatedUser, systemUserName);
return mockAuthenticationUtil;
}
/** /**
* Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has * Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has
* permissions. If the mock is asked for details about the user then it assumes the user is "admin". * permissions. If the mock is asked for details about the user then it assumes the user is "admin".
* <p>
* TODO: Change this method to private and this class to be a factory.
* *
* @param mockAuthenticationUtil The mock to initialise. * @param mockAuthenticationUtil The mock to initialise.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected static void setup(AuthenticationUtil mockAuthenticationUtil) public static void setup(AuthenticationUtil mockAuthenticationUtil)
{ {
setup(mockAuthenticationUtil, "admin", "admin", "admin"); setup(mockAuthenticationUtil, "admin", "admin", "admin");
} }
@@ -87,8 +52,6 @@ public class MockAuthenticationUtilHelper
/** /**
* Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has * Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has
* permissions. * permissions.
* <p>
* TODO: Change this method to private and this class to be a factory.
* *
* @param mockAuthenticationUtil The mock to initialise. * @param mockAuthenticationUtil The mock to initialise.
* @param adminUserName The name of the default admin user. * @param adminUserName The name of the default admin user.
@@ -96,7 +59,7 @@ public class MockAuthenticationUtilHelper
* @param systemUserName The name of the system user. * @param systemUserName The name of the system user.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected static void setup(AuthenticationUtil mockAuthenticationUtil, String adminUserName, public static void setup(AuthenticationUtil mockAuthenticationUtil, String adminUserName,
String fullyAuthenticatedUser, String systemUserName) String fullyAuthenticatedUser, String systemUserName)
{ {
reset(mockAuthenticationUtil); reset(mockAuthenticationUtil);