Java Reference
In-Depth Information
the database and the persistence layer. What's more, we left the original
applicationContext.xml
and
web.xml
unchanged. We have a neat separa-
tion between the application code and the test code.
Out-of-Container Testing with Mockito
Getting our tests to be executable without depending on the database
or the persistence layer is nice, as is using the Stripes mock objects
because we can test action bean features. However, you might like some
of your tests to be even more “stand-alone,” meaning that you can test
a class in isolation without having to set up the Stripes mock objects.
For example, consider testing the phone number type converter by
itself. We can write tests for the
convert
( ) method without any depen-
dencies:
package
stripesbook.test.plainmock;
import static
org.junit.Assert.
*
;
public class
PhoneNumberTypeConverterTest {
private
TypeConverter<PhoneNumber> typeConverter;
private
Collection<ValidationError> errors;
@Before
public void
setup() {
typeConverter =
new
PhoneNumberTypeConverterFormatter();
errors =
new
ArrayList<ValidationError>();
}
@Test
public void
testValidPhoneNumber() {
PhoneNumber phoneNumber = typeConverter.convert(
"(555) 444.6667", PhoneNumber.
class
, errors);
assertEquals(0, errors.size());
assertEquals("555", phoneNumber.getAreaCode());
assertEquals("444", phoneNumber.getPrefix());
assertEquals("6667", phoneNumber.getSuffix());
}
@Test
public void
testInvalidPhoneNumber() {
PhoneNumber phoneNumber = typeConverter.convert(
" 55 444.667 ", PhoneNumber.
class
, errors);
assertNull(phoneNumber);
assertEquals(1, errors.size());
}
}
Search WWH ::
Custom Search