Refactor tests to use Mockito instead of JMockit

This commit is contained in:
Tim Donohue
2020-01-21 15:59:26 -06:00
parent eb53950660
commit 6248867de7

View File

@@ -11,6 +11,8 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileInputStream;
@@ -21,24 +23,22 @@ import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import mockit.NonStrictExpectations;
import org.apache.logging.log4j.Logger;
import org.dspace.AbstractUnitTest;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.WorkspaceItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.util.ReflectionTestUtils;
/**
* Unit Tests for class InstallItem
@@ -57,6 +57,12 @@ public class InstallItemTest extends AbstractUnitTest {
private Collection collection;
private Community owningCommunity;
/**
* Spy of AuthorizeService to use for tests
* (initialized / setup in @Before method)
*/
private AuthorizeService authorizeServiceSpy;
/**
* log4j category
*/
@@ -78,6 +84,14 @@ public class InstallItemTest extends AbstractUnitTest {
this.owningCommunity = communityService.create(null, context);
this.collection = collectionService.create(context, owningCommunity);
context.restoreAuthSystemState();
// Initialize our spy of the autowired (global) authorizeService bean.
// This allows us to customize the bean's method return values in tests below
authorizeServiceSpy = spy(authorizeService);
// "Wire" our spy to be used by the current loaded workspaceItemService and collectionService
// (To ensure it uses the spy instead of the real service)
ReflectionTestUtils.setField(workspaceItemService, "authorizeService", authorizeServiceSpy);
ReflectionTestUtils.setField(collectionService, "authorizeService", authorizeServiceSpy);
} catch (SQLException | AuthorizeException ex) {
log.error("SQL Error in init", ex);
fail("SQL Error in init: " + ex.getMessage());
@@ -140,30 +154,22 @@ public class InstallItemTest extends AbstractUnitTest {
/**
* Test of installItem method (with an invalid handle), of class InstallItem.
*/
@Test
@Test(expected = AuthorizeException.class)
public void testInstallItem_invalidHandle() throws Exception {
//Default to Full-Admin rights
new NonStrictExpectations(authorizeService.getClass()) {{
// Deny Community ADD perms
authorizeService.authorizeActionBoolean((Context) any, (Community) any,
Constants.ADD);
result = false;
// Allow full Admin perms
authorizeService.isAdmin((Context) any);
result = true;
authorizeService.isAdmin((Context) any, (EPerson) any);
result = true;
}};
// Allow full Admin rights
when(authorizeServiceSpy.isAdmin(context)).thenReturn(true);
String handle = "123456789/56789";
// create two items for tests
context.turnOffAuthorisationSystem();
WorkspaceItem is = workspaceItemService.create(context, collection, false);
WorkspaceItem is2 = workspaceItemService.create(context, collection, false);
context.restoreAuthSystemState();
//Test assigning the same Handle to two different items
String handle = "123456789/56789";
installItemService.installItem(context, is, handle);
// Assigning the same handle again should throw a RuntimeException
thrown.expect(RuntimeException.class);
installItemService.installItem(context, is2, handle);
fail("Exception expected");
}