Using Scala-test FunSpec + Spring autowire

1,307 views
Skip to first unread message

Mobe

unread,
Mar 18, 2014, 12:27:25 PM3/18/14
to scalate...@googlegroups.com

Spring doesn't inject the
Neo4jTemplate template instance into my test class.
And I can't figure out why. It could be that Spring isn't picking up the spring configuration at all or
maybe there's another issue with FunSpec: http://stackoverflow.com/questions/6282471/how-do-i-integrate-scalatest-with-spring
To make sure it's not a problem with the
Neo4jTemplate itself, I also tried to autowire the TestPerson component.
Same thing though. Thanks!


@RunWith(classOf[JUnitRunner])
@ContextConfiguration(locations = Array("classpath:/simpleModuleContext.xml"))
class MovieRepositoryTest extends FunSpec with Matchers with MockFactory {

  private var template: Neo4jTemplate = _

  @Autowired
  def setTemplate(t:Neo4jTemplate) = template = t

  @Autowired
  var testperson: TestPerson = _


  describe("MovieRepository") {

    it("persistedMovieShouldBeRetrievableFromGraphDb") {

      testperson should not be null
      template should not be null

      val forrestGump = template.save(new Movie(title = "Forrest Gump", year = 1994))
      val retrievedMovie = template.findOne(forrestGump.nodeId, classOf[Movie]);

      assertResult(forrestGump.title)(retrievedMovie.title)
    }
  }
}

package de.xxx.springdata.types

import org.springframework.stereotype.Component

@Component
class TestPerson {}


simpleModuleContext.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/data/neo4j
        http://www.springframework.org/schema/data/neo4j/spring-neo4j-3.0.xsd">

    <context:component-scan base-package="de.xxx" />
    <context:component-scan base-package="de.xxx.springdata.types"/>
    <neo4j:config base-package="de.xxx.springdata.types" storeDirectory="target/graph.db"/>

</beans>
Message has been deleted

Mobe

unread,
Mar 18, 2014, 2:41:26 PM3/18/14
to scalate...@googlegroups.com

I replaced the JUnitRunner from scala-test for SpringJUnit4ClassRunner hoping for some magic, but instead, things got even worse: java.lang.Exception: No runnable methods.
Then I added a @Test annotation and switched from FunSpec to the JUnite-like FunSuite to no avail.

Next thing I did was, I removed the line containing "@RunWith(classOf[JUnitRunner])" from my test class, included the dependency for scalatest-maven-plugin into the project's pom.xml
and followed the advice from http://stackoverflow.com/questions/6282471/how-do-i-integrate-scalatest-with-spring
to include
TestContextManager like so

new TestContextManager(classOf[MovieRepositoryTest]).prepareTestInstance(this)
describe("MovieRepository") {
...
}

mvn clean -Dtest=MovieRepositoryTest test

*** RUN ABORTED ***
  java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.determineCommonAncestor(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Class;


At least, there are now some classpathscanning activities in the log.

I'd glad, if anyone could demonstrate, how to make spring and scala-test work.

Mobe

unread,
Mar 18, 2014, 3:54:48 PM3/18/14
to scalate...@googlegroups.com
@ContextConfiguration(locations = Array("classpath:/simpleModuleContext.xml"))
class MovieRepositoryTest extends FunSpec with Matchers with MockFactory  {

  @Autowired

  private var template: Neo4jTemplate = _

  new TestContextManager(classOf[MovieRepositoryTest]).prepareTestInstance(this)

  // http://www.scalatest.org/user_guide/using_matchers#checkingEqualityWithMatchers

  describe("MovieRepository") {

    it("persistedMovieShouldBeRetrievableFromGraphDb") {
      template should not be null
    }
  }

This works, at least for now.  As I said, I replaced the JUnitRunner annotation by switching to the scala-test maven pluging in my pom.xml just for convenience, this is optional though.
Having the TestContextManager is the most important part.

Besides, the last NoSuchMethodError exception wasn't related to scala-test. I had to upgrade from spring-core 3.2.3.RELEASE to 3.2.8.RELEASE because of
the class org.springframework.core.SpringProperties has been missing ( introduced in  3.2.7.RELEASE )




Bill Venners

unread,
Mar 18, 2014, 4:34:34 PM3/18/14
to scalate...@googlegroups.com
Hi Mobe,

Thanks for posting the solution. Sorry you had to work it out on your own, but I haven't tried using Spring with ScalaTest before. Please feel free to post anything else you figure out regarding Spring & ScalaTest for others that come after you and run into similar questions.

Bill


--
You received this message because you are subscribed to the Google
Groups "scalatest-users" group.
To post to this group, send email to scalate...@googlegroups.com
To unsubscribe from this group, send email to
scalatest-use...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/scalatest-users?hl=en
ScalaTest itself, and documentation, is available here:
http://www.artima.com/scalatest
---
You received this message because you are subscribed to the Google Groups "scalatest-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatest-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Bill Venners
Artima, Inc.
http://www.artima.com

Mobe

unread,
Mar 19, 2014, 2:03:12 PM3/19/14
to scalate...@googlegroups.com
Hi Bill,

np :)

A co-worker of mine ran into the same issue today and I was glad to help ;)
Where I work everyone uses autowired Spring tests since so many other framworks depond on it (Spring batch, Spring integration, Spring Data, etc..)
Maybe (if time permits it ) you could update the scalatest website with a small example? I think this will help a lot of people coming from java/spring.



Mounir
Reply all
Reply to author
Forward
0 new messages