Is there an alternative to mock objects in unit testing?

java, junit, unit-testing

Solution

Have you tried a dynamic mocking framework such as EasyMock? It does not require you to "create" a Mock object in that you would have to write the entire class - you specify the behavior you want within the test itself.

An example of a class that uses a `UserService` to find details about a User in order to log someone in:

//Tests what happens when a username is found in the backend
public void testLoginSuccessful() {
    UserService mockUserService = EasyMock.createMock(UserService.class);

    EasyMock.expect(mockUserService.getUser("aUsername")).andReturn(new User(...));
    EasyMock.replay(mockUserService);

    classUnderTest.setUserService(mockUserService);

    boolean isLoggedIn = classUnderTest.login("username");
    assertTrue(isLoggedIn);
}

//Tests what happens when the user does not exist
public void testLoginFailure() {
    UserService mockUserService = EasyMock.createMock(UserService.class);

    EasyMock.expect(mockUserService.getUser("aUsername")).andThrow(new UserNotFoundException());
    EasyMock.replay(mockUserService);

    classUnderTest.setUserService(mockUserService);

    boolean isLoggedIn = classUnderTest.login("username");
    assertFalse(isLoggedIn);
}

Problem

It's a Java (using JUnit) enterprise Web application with no mock objects pre-built, and it would require a vast amount of time not estimated to create them. Is there a testing paradigm that would give me "some" test coverage, but not total coverage?

Original source