Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Learn Scala with Specs2 Spring

Use Scala code to safely test and debug Spring-based Java apps

  • Print
  • Feedback

Page 5 of 5

Listing 7. Acceptance specification

@WebContextConfiguration(
        webContextLocations = Array("/WEB-INF/sw-servlet.xml"),
        contextLocations = Array("classpath*:/META-INF/spring/module-context.xml"))
    @DataSource(name = "java:comp/env/jdbc/test",
        driverClass = classOf[JDBCDriver], url = "jdbc:hsqldb:mem:test")
    @TransactionManager(name = "java:comp/TransactionManager")
    class IndexControllerTest extends Specification {
        @Autowired var riderManager: RiderManager = _

        "web roundtrip test" in {
            val rider = new Rider()
            rider.setUsername("aaaa")
            rider.setFullName("Jan")
            Json(put)("/riders.json", rider)

            val savedRider = riderManager.getByUsername("aaaa")
            val wo = Xhtml(get)("/riders/" + savedRider.getId + ".html")

            Xhtml(post)((wo!) << ("#fullName", "Edited Jan"))

            riderManager.getByUsername("aaaa").getFullName must_== ("Edited Jan")
        }

    }

The code in Listing 7 verifies that the system can accept an HTTP PUT to the /riders.html URL with some parameters. On receiving the PUT request, I verify that the system inserts the rider object specified in the parameters of the request. Once inserted, I verify that the system can also execute the HTTP GET request to load the newly created rider.

The result of loading the rider is an XHTML document. In order to verify that the document contains the correct information, I use a jQuery-style selector to modify the value of the input element with the id fullName. Once I have the XHTML document, I'll verify the HTTP POST of the XHTML form, which should result in an update to the rider object. Finally I'll check that the system has correctly saved the edits to the Rider object.

In conclusion

If you're already a Spring developer and you've been curious about incorporating Scala's functional programming features into your applications, then you can start by implementing your test code in Scala. Doing so will help you learn the patterns of object-functional programming without committing beginner code to your main code base. Specs2 Spring is a solid testing framework and could be a good starting point for this approach to learning Scala.

About the author

Jan Machacek is the technical director of Cake Solutions. He is a highly experienced Java enterprise architect, consultant, and developer with strong technical and team management skills. Jan has led teams through the perils of agile software delivery, bringing control and value to businesses and the joy of programming to technical teams. Alongside several articles, Jan has authored four books, including Pro Spring and Pro Spring 2.5. He regularly speaks at conferences and developer events in the UK and abroad and is the editor of the Open Source Journal. Jan leads three open source projects: Specs2 Spring, Spock Spring Integration, and Scalad. In his spare time, Jan competes in time trials and road races as a member of the Manchester Wheelers' Cycling Club.

Read more about Tools & Methods in JavaWorld's Tools & Methods section.

  • Print
  • Feedback

Resources
  • Specs2 Spring is a Specs2 extension for BDD & testing typical Spring enterprise applications in Scala.
  • The Spring framework is a de-facto standard for implementing loosely-coupled, flexible, and testable Java EE applications.
  • Specs2 is a Scala BDD testing framework.
  • Scala is a strongly-typed object-functional scalable language.
  • "Dependency injection vs. Cake pattern" (Cake Solutions Team Blog, December 15, 2011): Learn more about the cake pattern as an alternative to dependency injection, as it is implemented in several web application frameworks.

More from JavaWorld