|
Page 4 of 6 Here is the JTiger unit test fixture. Youll find that this fixture achieves 100% code coverage. That is to say, every single code instruction in PersonImpl.java is executed, even the NullPointerException! In fact, even both the first and the second part of the expression that must evaluate to true for the NullPointerException to occur is executed. package persons; import static persons.PersonImpl.getPerson; import static org.jtiger.assertion.Reflection.assertHasDeclaredConstructor; import static org.jtiger.assertion.Basic.assertFalse; import static org.jtiger.assertion.Basic.assertEqual; import static org.jtiger.assertion.Modifier.assertPrivate; import static org.jtiger.assertion.Serialization.assertSerializes; import static org.jtiger.assertion.Serialization.assertSerializesEqual; import static org.jtiger.assertion.ObjectFactoryContract.assertObjectFactoryFillsContract; import static org.jtiger.assertion.EqualsMethodContract.assertEqualsMethodFillsContract; import static org.jtiger.assertion.HashCodeMethodContract.assertHashCodeMethodFillsContract; import org.jtiger.assertion.ObjectFactory; import org.jtiger.framework.Fixture; import org.jtiger.framework.Test; import org.jtiger.framework.Category; import org.jtiger.framework.ExpectException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Constructor; // this annotation is optional @Fixture("Test PersonImpl") public final class TestPersonImpl { private TestPersonImpl() { } @Test @ExpectException(UnsupportedOperationException.class) @Category("factory") void factory() throws Throwable { final Constructor c = assertHasDeclaredConstructor(PersonImpl.class,null); assertPrivate(c); assertFalse(c.isAccessible()); c.setAccessible(true); try { c.newInstance(); } catch(InvocationTargetException e) { throw e.getCause(); } } @Test @Category("names") void names() { assertEqual(getPerson("first", "last").getFirstName() , "first","First name didn't match"); assertEqual(getPerson("first", "last").getLastName() , "last","Last name didn't match"); } @Test("null names 1") @ExpectException(NullPointerException.class) @Category("names") void nullNames1() { getPerson(null, "last"); } @Test("null names 2") @ExpectException(NullPointerException.class) @Category("names") void nullNames2() { getPerson("first", null); } @Test @Category("serialization") void serialization() { assertSerializes(getPerson("first", "last")); assertSerializesEqual(getPerson("first", "last")); } @Test @Category({"equals", "hashCode"}) void equalsHashCode() { final ObjectFactory factory1 = new ObjectFactory(){ public Person newInstanceX() { return getPerson("John", "Doe"); } public Person newInstanceY() { return getPerson("Mary", "Magdalene"); } }; final ObjectFactory factory2 = new ObjectFactory(){ public Person newInstanceX() { return getPerson("Jesus", "Magdalene"); } public Person newInstanceY() { return getPerson("Mary", "Magdalene"); } }; final ObjectFactory factory3 = new ObjectFactory(){ public Person newInstanceX() { return getPerson("Bob", "Down"); } public Person newInstanceY() { return getPerson("Bob", "Up"); } }; assertObjectFactoryFillsContract(factory1); assertEqualsMethodFillsContract(factory1); assertHashCodeMethodFillsContract(factory1); assertObjectFactoryFillsContract(factory2); assertEqualsMethodFillsContract(factory2); assertHashCodeMethodFillsContract(factory2); assertObjectFactoryFillsContract(factory3); assertEqualsMethodFillsContract(factory3); assertHashCodeMethodFillsContract(factory3); } }
|