mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 07:23:08 +00:00
Refactor tests to use Mockito instead of JMockit
This commit is contained in:
@@ -11,6 +11,8 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
|||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
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.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -21,24 +23,22 @@ import java.util.Calendar;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import mockit.NonStrictExpectations;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.dspace.AbstractUnitTest;
|
import org.dspace.AbstractUnitTest;
|
||||||
import org.dspace.authorize.AuthorizeException;
|
import org.dspace.authorize.AuthorizeException;
|
||||||
|
import org.dspace.authorize.service.AuthorizeService;
|
||||||
import org.dspace.content.factory.ContentServiceFactory;
|
import org.dspace.content.factory.ContentServiceFactory;
|
||||||
import org.dspace.content.service.CollectionService;
|
import org.dspace.content.service.CollectionService;
|
||||||
import org.dspace.content.service.CommunityService;
|
import org.dspace.content.service.CommunityService;
|
||||||
import org.dspace.content.service.InstallItemService;
|
import org.dspace.content.service.InstallItemService;
|
||||||
import org.dspace.content.service.ItemService;
|
import org.dspace.content.service.ItemService;
|
||||||
import org.dspace.content.service.WorkspaceItemService;
|
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.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit Tests for class InstallItem
|
* Unit Tests for class InstallItem
|
||||||
@@ -57,6 +57,12 @@ public class InstallItemTest extends AbstractUnitTest {
|
|||||||
private Collection collection;
|
private Collection collection;
|
||||||
private Community owningCommunity;
|
private Community owningCommunity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spy of AuthorizeService to use for tests
|
||||||
|
* (initialized / setup in @Before method)
|
||||||
|
*/
|
||||||
|
private AuthorizeService authorizeServiceSpy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* log4j category
|
* log4j category
|
||||||
*/
|
*/
|
||||||
@@ -78,6 +84,14 @@ public class InstallItemTest extends AbstractUnitTest {
|
|||||||
this.owningCommunity = communityService.create(null, context);
|
this.owningCommunity = communityService.create(null, context);
|
||||||
this.collection = collectionService.create(context, owningCommunity);
|
this.collection = collectionService.create(context, owningCommunity);
|
||||||
context.restoreAuthSystemState();
|
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) {
|
} catch (SQLException | AuthorizeException ex) {
|
||||||
log.error("SQL Error in init", ex);
|
log.error("SQL Error in init", ex);
|
||||||
fail("SQL Error in init: " + ex.getMessage());
|
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 of installItem method (with an invalid handle), of class InstallItem.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test(expected = AuthorizeException.class)
|
||||||
public void testInstallItem_invalidHandle() throws Exception {
|
public void testInstallItem_invalidHandle() throws Exception {
|
||||||
//Default to Full-Admin rights
|
// Allow full Admin rights
|
||||||
new NonStrictExpectations(authorizeService.getClass()) {{
|
when(authorizeServiceSpy.isAdmin(context)).thenReturn(true);
|
||||||
// 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;
|
|
||||||
}};
|
|
||||||
|
|
||||||
String handle = "123456789/56789";
|
// create two items for tests
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
WorkspaceItem is = workspaceItemService.create(context, collection, false);
|
WorkspaceItem is = workspaceItemService.create(context, collection, false);
|
||||||
WorkspaceItem is2 = workspaceItemService.create(context, collection, false);
|
WorkspaceItem is2 = workspaceItemService.create(context, collection, false);
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
|
||||||
//Test assigning the same Handle to two different items
|
//Test assigning the same Handle to two different items
|
||||||
|
String handle = "123456789/56789";
|
||||||
installItemService.installItem(context, is, handle);
|
installItemService.installItem(context, is, handle);
|
||||||
|
|
||||||
// Assigning the same handle again should throw a RuntimeException
|
// Assigning the same handle again should throw a RuntimeException
|
||||||
thrown.expect(RuntimeException.class);
|
|
||||||
installItemService.installItem(context, is2, handle);
|
installItemService.installItem(context, is2, handle);
|
||||||
fail("Exception expected");
|
fail("Exception expected");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user