Using Specs2 with Spring

208 views
Skip to first unread message

Steven Marcus

unread,
May 25, 2011, 11:18:17 PM5/25/11
to specs2...@googlegroups.com
Spring supplies SpringJUnit4ClassRunner and AbstractJUnit4SpringContextTests to make it easier to bootstrap a spring context in your JUnit tests.

I've been using scalatest where

class Test extends AbstractJUnit4SpringContextTests with JUnitSuite {
}

works well with the SpringJUnit4ClassRunner -- because scalatest's JUnitSuite just adds some sugar to what is otherwise a JUnit test.

Leveraging SpringJUnit4ClassRunner and AbstractJUnit4SpringContextTests doesn't appear possible with Specs2.
Anyone have experience integrating Specs2 with Spring?

thanks!
S

etorreborre

unread,
May 26, 2011, 1:00:47 AM5/26/11
to specs2-users
Hi Steven,

I don't know much about Spring unfortunately.

My understanding is that:

- a SpringJUnit4ClassRunner creates a TestContextManager
- that manager injects objects with the prepareInstance method

What you can try is to:

- add an initial step to prepare the specification:
override def map(fs: =>Fragments) = step(prepareInstance(this)) ^
fs

But I don't really know how this can really work.

One other option is to go on working with your usual JUnit test cases
but import the JUnit specs2 expectations:

http://etorreborre.github.com/specs2/api/index.html#org.specs2.matcher.JUnitMustExpectations

I'd be happy if someone really Spring could have a look at that!

Eric.

etorreborre

unread,
Jun 8, 2011, 7:40:12 PM6/8/11
to specs2...@googlegroups.com
Hi Steven,

There was a question asked recently on SOF about ScalaTest + Spring: http://stackoverflow.com/questions/6282471/how-do-i-integrate-scalatest-with-spring

The follow-up discussion on the scalatest mailing-list may be relevant as well.

Cheers,

Eric.

Fernando Racca

unread,
Jun 9, 2011, 6:19:02 PM6/9/11
to specs2-users
leveraging the spring context support given by the test context
framework is pretty fundamental for integration testing of large
applications.

is there any chance to have that as a priority for next releases?

Fernando Racca

On Jun 9, 12:40 am, etorreborre <etorrebo...@gmail.com> wrote:
> Hi Steven,
>
> There was a question asked recently on SOF about ScalaTest + Spring:http://stackoverflow.com/questions/6282471/how-do-i-integrate-scalate...
>
> The follow-up discussion<https://groups.google.com/d/topic/scalatest-users/dI44ogJg-IA/discussion>on the scalatest mailing-list may be relevant as well.
>
> Cheers,
>
> Eric.

etorreborre

unread,
Jun 9, 2011, 6:47:45 PM6/9/11
to specs2...@googlegroups.com
Hi Fernando, 

I don't have enough Spring experience but have you tried the SOF tip:

class SpringSpec extends mutable.Specification {
  "first example" in new spring {
    app must not be(null)
  }
}

@ContextConfiguration(
  locations = Array("myPackage.UnitTestSpringConfiguration"), 
  loader = classOf[AnnotationConfigContextLoader])
trait spring extends Scope {
  
  @Autowired val app: WebApplication = null
  @Autowired val siteDAO: SiteDAO = null
  new TestContextManager(this.getClass()).prepareTestInstance(this)
}

Would that work for you? What would you add?

Anatoly Rabinovich

unread,
Jul 30, 2015, 11:49:29 AM7/30/15
to specs2-users, steven...@gmail.com
I have the same issue (unless it was fixed and I'm not aware of that). Defaulted to using ScalaTest. So, I'm bumping this up in case anyone came up with a solution in the past four years.
Reply all
Reply to author
Forward
0 new messages