Showing posts with label Unit Test. Show all posts
Showing posts with label Unit Test. Show all posts

2008/04/20

Acceptance Testing Application Behavior

Venkat Subramaniam on Seattle NFJS Java Conference


How do you ensure your applications meet the expectations of your key customers?

Agile Development, it's all about building relevant working software by constantly getting feedback. Testing is a key ingredient in Agile development. 

Type of tests and Level: 
- UI/Presentation: Watir, Selenium
- Controls/Services: BDD/FIT
- Classes/Models: Unit Testing

Gathering requirement is a challenge. Use case helps, but often tend to be heavy weight and in effective beyond certain point of diminishing returns. Agile development use User Stories. When creating User Stories, 3Cs is important: 
- Card: Feature expressed in an index card
- Conversation: Use short description as starting point for useful discussions that promote exploration and understanding
- Confirmation: Tests are written as a way to confirm completion of feature development

INVEST in User Stories:
- Independent
- Negotiable
- Valuable
- Estimable
- Small
- Testable

FIT: Framework for Integration Testing
- General purpose open-ended framework for expressing tests
- Help focus on business perspectives
- Easy for non-programmers to use (Table represent tests)
- Automated checking and reporting of results
- Useful for Business Rules related to business calculation  and business process/workflow

How does it FIT together?
- Table of Tests, expresses expectations by way of examples
- Fixture, check that the system satisfied the given tests
- Column Fixture, helps test calculations
- Action Fixture, helps test events or actions
- Row Fixture, helps test collections

Tables are in pure html, which includes the test that the fixture will be used to execute on. Fixture is glue code in java that extends from FIT abstract Fixtures.

FitNesse provides a single web based UI for developing, running and viewing results of test. 

BDD - Behavior Driven Design 

- Each behavior is expressed as a test/exercise method
- It tells what the object should do 
- Java Tools for BDD: JBehave, JDave, beanSpec, Instinct
- Groovy Tools for BDD: GSpect, easyb

Reference:

2007/10/12

SpringContextAware JUnit TestCase

In order to run Junit test case outside of a J2EE container, the test case need to initialize the Spring framework properly. I created this abstract class SpringContextAware, which extends from junit.framework.TestCase and initialize the Spring framework in it's setUp() method. User Test Cases that extends from this class will be running with Spring context loaded already.

public abstract class SpringContextAware extends TestCase {
public SpringContextAware(String name){
super(name);
}

public void setUp() throws Exception{
if (SpringApplicationContext.getApplicationContext()==null) {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{
// list your context files here
"context-service.xml",
"context-hibernate.xml"
});
ctx.registerShutdownHook();
}
}

public void tearDown() throws Exception{
}
}


Here is the SpringApplicationContext class, basically it allows java classes not defined as a spring bean to be able to access the Spring Application Context.

public class SpringApplicationContext implements ApplicationContextAware {

private static ApplicationContext context;

public static ApplicationContext getApplicationContext() {
return SpringApplicationContext.context;
}

/**
* This method is called from within the ApplicationContext once it is
* done starting up, it will stick a reference to itself into this bean.
*
* @param context a reference to the ApplicationContext.
*/
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringApplicationContext.context = context;
}

/**
*This is about the same as context.getBean("beanName"), except it has its
* own static handle to the Spring context, so calling this method statically
* will give access to the beans by name in the Spring application context.
* As in the context.getBean("beanName") call, the caller must cast to the
* appropriate target class. If the bean does not exist, then a Runtime error
* will be thrown.
*
* @param beanName the name of the bean to get.
* @return an Object reference to the named bean.
*/
public static Object getBean(String beanName) {
return context.getBean(beanName);
}
}


This bean itself however, needs to be defined in the Spring Context XML file:

<!-- provide access to spring application context -->
<bean id="springApplicationContext" class="com.blah.blah.SpringApplicationContext">
</bean>


Any comments are welcome :)

Well well... why another J2EE blog? I benefited from other people's technical blogs, and guess what, it's a good idea to contribute some of my works too. Hope it's helpful and useful, to all of your folks.