Java Reference
In-Depth Information
19.2.1
Unitils EasyMock support
Before we dig into Unitils mock support, let's first see how Unitils works so we can
configure it properly.
Unitils is configured through standard Java properties (those defined by a pair of
strings in the form name=value ), and these properties can be defined in three distinct
files. The first file is called unitils-default.properties, and it's provided on Unitils' JAR . 5
As the name implies, it provides default values for most of the properties, so Unitils
could be used out of the box without custom configuration. But default properties
aren't always enough—if they were, there'd be no need for properties at all—so Unit-
ils allows project-specific properties to be overridden in a unitils.properties file, which
should be available either in the classpath or in the user's home directory. 6 Finally,
Unitils also allows each developer to override the properties in a user-specific file
called unitils-locals.properties, 7 which should also be present in the classpath or user's
home directory.
This configuration mechanism allows a high degree of flexibility, which can be
dangerous. If tests rely too heavily on the user-specific properties, they might be hard
to reproduce. Ideally, the whole test suite should be runnable using only the project-
specific properties, and the user-specific ones should be used only in some particular
cases, like when each user has its own testing database.
That being said, each module has its own properties, and even which modules are
available are defined by a property, unitils.modules , which by default includes all
modules. For our mock example (listing 19.1), it isn't necessary to change any proper-
ties, although the mock configuration is quite extensive (two modules are involved, as
you can see in the listing).
Listing 19.1 UserFacadeImpl test refactored to use Unitils mock support
[...]
import static com.manning.junitbook.ch19.model.EntitiesHelper.*;
import static org.unitils.easymock.EasyMockUnitils.replay;
import static org.easymock.EasyMock.expect;
B
import org.junit.Test;
import org.junit.runner.RunWith;
import org.unitils.UnitilsJUnit4TestClassRunner;
import org.unitils.easymock.annotation.Mock;
import org.unitils.inject.annotation.InjectIntoByType;
import org.unitils.inject.annotation.TestedObject;
import com.manning.junitbook.ch19.dao.UserDao;
import com.manning.junitbook.ch19.model.User;
import com.manning.junitbook.ch19.model.UserDto;
5
The contents of this file are also documented online, at http://unitils.org/unitils-default.properties.
6
The home directory is defined by the Java system property user.home .
7
The name of this file is defined itself by a property, so it could have any name, as long as the name property
is changed in unitils.properties.
 
 
 
 
 
 
 
Search WWH ::




Custom Search