Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Adding examples of unit test cases (FCREPO-1584)

We like tests. We use JUnit and Mockito (and, sometimes, reluctantly, PowerMockito.) HTTP Integration tests use grizzly. 

Writing tests

Unit Tests

Each module has unit tests in the src/test/java/org/fcrepo/ directory and integration tests in the src/test/java/org/fcrepo/integration/ directory.

The unit tests often use mock objects to create surrogates for the Modeshape and webapp machinery that much of the Fedora code interacts with.  Some good examples of mock object usage are in the fcrepo-http-api module (e.g., FedoraNodesTest.java) and in the fcrepo-jms-indexer-core module (e.g. IndexerGroupTest.java). By convention, unit test classes are named as: <FunctionalClass>Test.java

Examples

Code Block
languagejava
titlefcrepo-kernel/src/test/java/org/fcrepo/kernel/observer/FedoraEventTest.java
firstline114
linenumberstrue
collapsetrue
@Test

    public void testAddType() {

        e.addType(PROPERTY_CHANGED);

        assertEquals(2, e.getTypes().size());




        assertTrue("Should contain: " + PROPERTY_CHANGED, contains(e.getTypes().iterator(), PROPERTY_CHANGED));

        assertTrue("Should contain: 1", contains(e.getTypes().iterator(), 1));

    }




    @Test

    public void testAddProperty() {

	e.addProperty("prop");

        assertEquals(1, e.getProperties().size());

        assertEquals("prop", e.getProperties().iterator().next());

    }




    @Test

    public void testToString() throws RepositoryException {

        final String text = e.toString();

        assertTrue("Should contain path: " + text, text.contains(e.getPath()));

        assertTrue("Should contain info: " + text, text.contains(e.getInfo().toString()));




        assertTrue("Should contain types: " + text, text.contains(Integer.toString(e.getTypes().iterator().next())));

        assertTrue("Should contain date: " + text, text.contains(Long.toString(e.getDate())));




        assertFalse("Should not contain user-data: " + text, text.contains(e.getUserData()));

        assertFalse("Should not contain user-id: " + text, text.contains(e.getUserID()));

    }
Code Block
languagejava
titlefcrepo-http-api/src/test/java/org/fcrepo/http/api/FedoraNodesTest.java
firstline159
linenumberstrue
collapsetrue
    @Test

    public void testCopyObject() throws RepositoryException, URISyntaxException {




        final ValueFactory mockVF = mock(ValueFactory.class);

	when(mockSession.getValueFactory()).thenReturn(mockVF);

        when(mockNodes.exists(mockSession, path)).thenReturn(true);

	when(mockContainer.getPath()).thenReturn(path);




        testObj.copyObject("http://localhost/fcrepo/bar");

	verify(mockNodes).copyObject(mockSession, path, "/bar");

    }




    @Test(expected = ClientErrorException.class)

    public void testCopyMissingObject() throws RepositoryException, URISyntaxException {




        final ValueFactory mockVF = mock(ValueFactory.class);

        when(mockSession.getValueFactory()).thenReturn(mockVF);

	when(mockNodes.exists(mockSession, path)).thenReturn(false);




	testObj.copyObject("http://localhost/fcrepo/bar");

    }




    @Test(expected = ServerErrorException.class)

    public void testCopyObjectWithBadDestination() throws RepositoryException, URISyntaxException {

        final ValueFactory mockVF = mock(ValueFactory.class);

        when(mockSession.getValueFactory()).thenReturn(mockVF);

	when(mockNodes.exists(mockSession, path)).thenReturn(true);




        testObj.copyObject("http://somewhere/else/baz");




    }

Integration Tests

Integration tests use embedded Modeshape, servlet engine, etc. to run real requests against the code.  The web-based tests typically use HttpClient to make requests.  Non-web tests, use injection to have the repository and service objects made accessible to the tests (the embedded repository and injection are configured in the src/test/resources directory).  Some good examples are in the fcrepo-http-api module (e.g. FedoraNodesIT.java) and fcrepo-kernel module (e.g., FedoraResourceImplIT.java). By convention, integration test classes are named as: <FunctionalClass>IT.java

Examples

 

 

Remember to follow the Code Style Guide when writing your test classes. This is especially true if writing a test class is your first foray into contributing to Fedora.

...