Search code examples
javaspringguiceplayframework-2.4scala-ide

Testing playframework 2.4 with spring-data injection out of Eclipse Scala IDE


I want to learn and try to set up a project using

  • play framework 2.4
  • JAVA
  • Spring data
  • Eclipse IDE

I took 'my inspiration' from

  1. play-scala-spring-data-jpa showing how to load spring data via guice.
  2. multiproject showing sub-project configuration (as I would like to use at least two applications which have a common code base, but should be packaged separately, to be able to be deployed to different machines)

I managed to successfully activator run my first sub-project, which is a JAVA-port of play-scala-spring-data-jpa. The injection magic from spring data works for the provided example (models.PersonRepository) as it should.

Now I'm stuck when it comes to testing. I can't manage to set up a test class and inject the models.PersonRepository, when I want to load and test a controller class (e.g. a functional test of controllers.Application), but only when running the test(s) of my sub-project core out of Eclipse IDE (core is the eclipse project created by executing activator eclipse). Doing activator test works and the test shown below runs successfully.

public class ApplicationTest extends WithApplication {

    @Inject private PersonRepository repo;

    @Inject Application application;

    @Before
    public void setup() {
        // Here supposes to happen more to make PersonRepository injection work when running JUnit tests out of Eclipse IDE
        GuiceApplicationBuilder builder = new GuiceApplicationLoader()
            .builder(new Context(Environment.simple()));
        Guice.createInjector(builder.applicationModule()).injectMembers(this);
    }

    @After
    public void teardown() {
      Helpers.stop(application);
    }

    @Test
    public void testInjection() {
        assertNotNull(repo);
    }

Although I was working through the testing-related pages of playframework manual (starting here), I couldn't find anything helpful.

As I receive that error when testing out of Eclipse IDE,

com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for models.PersonRepository was bound.
  while locating models.PersonRepository
    for field at core.ApplicationTest.repo(ApplicationTest.java:32)
  while locating core.ApplicationTest

so I think the problem is, that I have to refer my sub-project's application config (core/conf/core-application.conf) which enables Guice Spring module for my sub-project. Secondly, I have to make sure, that configs.AppConfig is loaded appropriately, as this class manages spring data configuration.

As I am absolutely new to all this, maybe one could help me out, showing

  1. what to do, to make my tests also work when running out of Eclipse IDE (Scala IDE plugin)
  2. how to force the provided test class to use a dedicated in-memory test database (necessarily not using the one defined for my sub-project core/conf/META-INF/persistence.xml).

Full source code here

EDIT I've found out, that classpath differs when using JUnit test from Eclipse IDE instead of activator test, so I assume this is the reason why injection does not work.

# Classpath output was sorted before
$ diff activatorCP eclipseCP
0a1
> 
88,89d88
< /h/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.6.jar
< /h/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.6.jar
130,136c129,131
< /h/.sbt/boot/scala-2.10.5/org.scala-sbt/sbt/0.13.9/test-agent-0.13.9.jar
< /h/.sbt/boot/scala-2.10.5/org.scala-sbt/sbt/0.13.9/test-interface-1.0.jar
< /h/workspace/springtest/core/target/scala-2.11/classes/
< /h/workspace/springtest/core/target/scala-2.11/test-classes/
< /h/workspace/springtest/core/target/web/classes/main/
< /h/workspace/springtest/core/target/web/classes/test
< /h/workspace/springtest/core/target/web/public/test/
---
> /h/workspace/springtest/core/bin/
> /opt/eclipse-mars/configuration/org.eclipse.osgi/213/0/.cp/
> /opt/eclipse-mars/configuration/org.eclipse.osgi/214/0/.cp/

As I've created eclipse's project by using activator compile eclipse, I wonder what else do I have to do, to get matching classpaths


Solution

  • The difference in behavior came from customizing JAVA options in project's build.sbt allowing activator test to run correctly. These customization have to manually be carried over to eclipse project's Run Configuration. After that, tests out of eclipse IDE also run, successfully.