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.

3 comments:

  1. Hi there! “Can you write my essay.com for me free?” We can help you at a reasonable price. If you’re having trouble getting started with your piece, an assistant on this platform will prove helpful. They can create an outline for you for free and offer tons of advice to get you moving.

    ReplyDelete
  2. A customer can enjoy coupons throughout their stay with the service. The first discount is usually on your first order and can only be redeemed once https://expertpaperwriter.com/studybay-com-review/. After that, you may enjoy regular promotions as they come. As long as you fall in the said category, you may always take advantage of discounts to save on the price of your order.

    ReplyDelete
  3. Mighty Mount Coupons are a great way to save money on a variety of versatile and durable mounting solutions. Mighty Mount offers reliable and innovative options for securing your smartphone, tablet, GPS or other devices in your car, home or during outdoor adventures.

    With the help of Mighty Mount Coupons, customers can get these practical solutions at discounted prices which make it more affordable to organize and secure their devices wherever they go. These discounts provide an excellent opportunity to try out the convenience and reliability of Mighty Mount products while saving money on purchases.

    ReplyDelete