2012-04-03

Scala + Guice + Gradle: Integration tests (Part 5)

Previously I showed you how to add to your Scala + Guice project JPA 2 implemented by EclipseLink. In this article I will show you how to integration test it using the specs library.

build.gradle

Below are the relevant part of your build.gradle file.
test { exclude "**/*IT.class" }

task integrationTest(type: Test, dependsOn: testClasses) { include "**/*IT.class" }

check.dependsOn integrationTest

dependencies {
    testCompile group: 'org.scala-tools.testing', name: 'specs_2.9.1', version: '1.6.9'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}
(This obviously is not the complete file.)

We specified a new task called integrationTest which will run our integration tests. It will run during gradle build as well. gradle test alone will NOT run them.

Step by step - PersistenceLibraryRepositorySpecIT.scala

package me.m1key.audioliciousmigration.repository

import org.specs._
import org.specs.runner._
import org.junit.runner.RunWith
import me.m1key.audioliciousmigration.persistence.JpaPersistenceProvider
import me.m1key.audiolicious.domain.entities.Library
import java.util.Date

@RunWith(classOf[JUnitSuiteRunner])
class PersistenceLibraryRepositorySpecIT extends Specification with JUnit {

 val jpaPersistenceProvider = new JpaPersistenceProvider
 jpaPersistenceProvider.initialise
 val repository = new PersistenceLibraryRepository(jpaPersistenceProvider)
 val entityManager = jpaPersistenceProvider.getEntityManager

//...
This is the test declaration. It allows us to run the test as a JUnit test from the IDE. Notice that file name ends with ...IT.scala. This is how we differentiate integration tests.

Step by step - before

// ...
doBeforeSpec {
   deleteLibraries
 }
//...
This will run before the test (just once).

Step by step - test


 "Fetching latest library with no libraries" should {
    var library: Library = null

    doFirst {
     println("Preparing test 1...")
     deleteLibraries
     println("Test prepared. Libraries: %d".format(librariesCount))
    }

    "return None." in {
     entityManager.getTransaction().begin()
     repository.getLatestLibrary() mustBe None
     entityManager.getTransaction().commit()
    }

    doLast {
     println("Cleaning up...")
     deleteLibraries
     println("Cleaned up. Libraries: %d".format(librariesCount))
    }
 }
This is a test. Notice the BDD style. The assertion is this line: repository.getLatestLibrary() mustBe None. doFirst and doLast allow us to prepare the test and clean up.

Step by step - another test

 "Fetching library by UUID with three libraries" should {
    setSequential()
    var olderLibrary: Library = null
    var anotherOlderLibrary: Library = null
    var newerLibrary: Library = null

    doFirst {
     println("Preparing test 3...")
     deleteLibraries
     olderLibrary = insertLibrary
     Thread.sleep(1000)
     anotherOlderLibrary = insertLibrary
     Thread.sleep(1000)
     newerLibrary = insertLibrary
     println("Test prepared. Libraries: %d".format(librariesCount))
    }

    "return correct 1st library." in {
     entityManager.getTransaction().begin()
     val library = repository.getLibrary(olderLibrary.getUuid).get
     entityManager.getTransaction().commit()
     library mustBe olderLibrary
    }

    "return correct 2nd library." in {
     entityManager.getTransaction().begin()
     val library = repository.getLibrary(anotherOlderLibrary.getUuid).get
     entityManager.getTransaction().commit()
     library mustBe anotherOlderLibrary
    }

    "return correct 3rd library." in {
     entityManager.getTransaction().begin()
     val library = repository.getLibrary(newerLibrary.getUuid).get
     entityManager.getTransaction().commit()
     library mustBe newerLibrary
    }

    doLast {
     println("Cleaning up...")
     deleteLibraries
     println("Cleaned up. Libraries: %d".format(librariesCount))
    }
 }
setSequential allows us to keep variables (olderLibrary, anotherOlderLibrary, newerLibrary) in scope for all tests.

Step by step - final clean up

//...  
 doAfterSpec {
    deleteLibraries
 }
//...

You can see the whole source for this file here.

Final note

gradle build will NOT run tests that have already run since the last code change! If you want them to run again, use gradle clean build.

Download source code

Source code for this article can be obtained via GitHub. Please see the README file for building and running.

No comments:

Post a Comment