Skip to content

Instantly share code, notes, and snippets.

@krimple
Created August 23, 2011 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krimple/1165274 to your computer and use it in GitHub Desktop.
Save krimple/1165274 to your computer and use it in GitHub Desktop.
A Spock integration test against a Roo entity and the persistence context
import org.springframework.test.context.ContextConfiguration
import org.test.spock.model.Course
import javax.persistence.PersistenceContext
import javax.persistence.EntityManager
import org.junit.runner.RunWith
import org.spockframework.runtime.SpockRuntime
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.test.annotation.Rollback
import org.springframework.transaction.annotation.Transactional
@ContextConfiguration("classpath:/META-INF/spring/applicationContext.xml")
class CourseSpockTest extends spock.lang.Specification {
@PersistenceContext
private EntityManager em;
@Autowired
ApplicationContext context;
def "should be able to persist" () {
given:
Course course = new Course()
when:
expect:
assert context != null
assert em != null
assert course.entityManager() != null
}
@Transactional
@Rollback
def "trial persist" () {
given:
Course course = new Course();
when:
course.setName("Big Course")
course.setNumDays(5)
course.persist()
then:
course.getId() != null
course.entityManager().clear()
Course c2 = Course.findCourse(course.getId())
c2.getName().equals(course.getName())
c2.getNumDays() == course.getNumDays()
println course
}
}
@krimple
Copy link
Author

krimple commented Aug 23, 2011

This is awesome. I have this working at this point, and am working on an add-on that will enable this for Roo 1.2 (and perhaps 1.1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment