Java Reference
In-Depth Information
values[i++] = newValue;
}
} else {
throw new IllegalStateException( "No context on thread" );
}
super .row(values);
}
}
We start by creating a new EL context object B before each test and making it avail-
able through the method getContext() E , so the test cases could bind more objects
to the context as needed. Using the EL context, the id is bound C before the setup
dataset is read and bound again D before the assert dataset is read. This is necessary
because the test case might have changed the id (like on testAddUser() ), and the id
is represented by a primitive type (if it was a mutable object, this second bind would
not be necessary).
In the method getReplacedDataSet() F , the only relevant change (aside from
the absence of the id parameter) is that it now uses a custom dataset ( ELAware-
FlatXmlDataSet ).
In the class ELAwareFlatXmlDataSet G , we override the method row() , such that
it passes each dataset value to the EL engine for evaluation. The code at H shows a
subtle trick: instead of passing the EL context as a parameter to ELAwareFlatXmlData-
Set constructor, it's accessed with a call to getContext() . This is necessary because
row() is used during XML parsing, and FlatXmlDataSet parses the XML in the con-
structor. This is a bad practice on DbUnit's part—a constructor shouldn't call meth-
ods that can be overridden by subclasses.
We note the optimization at I : if the value isn't enclosed in ${} , there's no need
to evaluate it.
Finally, we get to where the EL engine does its job of evaluating the expression J ,
according to the values bound in the context.
Listing 17.24 shows the new test case. Notice that the only differences from the
previous version (listing 17.20) are the superclass and the dataset being used (which is
shown in listing 17.25).
Listing 17.24 UserDaoJdbcImplELTest using custom annotations
[...]
public class UserDaoJdbcImplAnnotationTest extends
AbstractDbUnitELTemplateTestCase {
@Test
@DataSets(setUpDataSet="/user-EL.xml")
public void testGetUserById() throws Exception {
User user = dao.getUserById(id);
assertUser(user);
}
@Test
@DataSets(assertDataSet="/user-EL.xml")
 
 
 
Search WWH ::




Custom Search