Latest is version 0.5 (released on 18th April 2012)
The Specs2 Spring Extension includes code that will help you set up the context for the entire integration test—and by context, we mean the appropriate entries in the JNDI environment as well as the beans under test, autowired in to the instance of the test under execution. Finally, Specs2 Spring can be easily configured to run every example in its own transaction that rolls back automatically when the example completes.
The extension is meant to help you write tests in Scala to test your Spring code (whether implemented in Java or Scala). You will be able to take advantage of all the features of the Specs2 framework and apply them to the Spring test code.
Why bother, you ask? Because Specs2 and Scala allow you to be much more expressive in your tests. Consequently, your tests can focus on the essence of what is being tested, reducing the noise that the traditional Java code requires. A motivational example shows how to prepare test data, insert them to the RDBMS and then verify that some service method works as expected.
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.orm.hibernate3.HibernateTemplate
import org.specs2.spring.{BeanTables, HibernateDataAccess, Specification}
import org.hibernate.SessionFactory
@IntegrationTest
class SomeComponentSpec extends Specification
with HibernateDataAccess with BeanTables {
@Autowired var someComponent: SomeComponent = _
@Autowired implicit var sessionFactory: SessionFactory = _
* Shows the usage of BeanTables and HibernateDataAccess to
* set up and insert test objects using the convenient tabular notation.
*/
"getByUsername finds existing Rider" in {
"age" | "username" | "name" | "teamName" |
32 ! "janm" ! "Jan" ! "Wheelers" |
30 ! "anic" ! "Ani" ! "Team GB" |> insert[Rider]
this.someComponent.getByUsername("janm").getName must_== ("Jan")
}
}
There are several things at play here: the custom IntegrationTest annotation defines the environment for the Spring components under test, the Autowired annotation on the someComponent variable tells Specs2 Spring to inject the constructed bean. Finally, the example sets up some test data using BeanTables, bulk-inserts them using the Hibernate ORM (method insert[T]: (T => Result) in HibernateDataAccess. Once the set-up work is done, we proceed to verify the correct behaviour of the getByUsername method in SomeComponent. Notably, the “getByUsename finds existing Rider” example runs in its own transaction. When the example completes (whether successfully or unsuccessfully), the transaction is rolled back!
The equivalent Java code would be much, much longer: you would be able to use the spring-test artifact, giving you the ability to inject dependencies into your test and run the test methods in their own transactions. Unfortunately, you would still be left to your own devices to set up the JNDI environment (which is non-trivial and very repetitive work). Moreover, the body of the test method would contain much more noise, distracting you from the test code.
Specs2 Spring contains support for:
- Multiple XA DataSources to RDBMS as javax.sql.DataSource,
- Single XA transaction support as javax.transaction.UserTransaction,
- Multiple JMS queue and topic support javax.jms.Queue, javax.jms.Topic, javax.jms.ConnectionFactory,
- Multiple Javamail as javax.mail.Session,
- Multiple WorkManagers—both the commonj.work.WorkManager and the javax.spi.resource.work.WorkManager,
- Multiple arbitrary beans, as long as the types include accessible nullary constructor,
- Ability to register a class that can inject arbitrary entries into the JNDI environment.
Resources
- Submit & review improvements, new feature requests and bugs at https://github.com/janm399/specs2-spring/issues
- The sources are available at https://github.com/janm399/specs2-spring
- Compiled artefacts are available from Maven Central