Java J2EE Portal
Enterprise Java Station
J2EE curve
Java News / Articles
Java News / Articles
spring Tutorial
Building Web Applications Using The Spring Framework
Simplified BPM integration with JBoss jBPM
Continuous Integration - Agile Software Development Q & A
Processing...
Buy Java, Deals On Software Technology Store
Click here for great deals on computers, laptops, software and books
Why pick JTiger Java Unit Testing Framework over good old JUnit? PDF Print
Oct 05, 2005 at 05:37 AM

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);
}
}




Add This Feed Button

Enter your Email


Java Expert Interviews
Direct Web Remoting (DWR) Creator - Joe Walker
With Direct Web Remoting (DWR) unnecessary complexity is a bug
Pradeep Chopra Whizlabs
Certifying your way to success
RoelStalmanOracleJDeveloper
JDeveloper is the most comprehensive Java IDE available
Processing...
Go to top of page  Home |
SiteMap

Copyright 2004 to 2008 Rightrix Solutions. All rights reserved. All product names are trademarks of their respective companies. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Rightrix Solutions and IndicThreads.com are independent of Sun Microsystems, Inc.

Views expressed at IndicThreads.com reflect the views of the authors alone, and do not necessarily reflect those of IndicThreads.com. IndicThreads.com and it's authors are not responsible for reader comments and opinions.

Enterprise Java J2EE JEE Portal >> IndicThreads.com