scalacenter / bloop-maven-plugin

Maven Plugin for Bloop

Home Page:https://scalacenter.github.io/bloop/docs/build-tools/maven

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

maven-bloop: JUnit tests don't work out of the box

nigredo-tori opened this issue · comments

Symptoms

After importing a Maven project to Metals, it doesn't show lenses for JUnit test classes, as well as for ZIO specs which piggyback on JUnit machinery as described here.

Reproduction

(This example focuses on running tests via Bloop CLI, but the root reason is the same, and fixing one leads to fixing the other)

Consider this Maven project: example.zip. It includes a single
failing test that requires JUnit to run. Maven understands this:

$ mvn test
...
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
...

After importing the project to Bloop as described here, we can attempt to run the tests, but Bloop doesn't recognize them:

$ bloop test my-artifact-test
...
[E] Missing configured test frameworks in my-artifact-test
===============================================
Total duration: 0ms
No test suites were run.
===============================================

The missing piece seems to be the JUnit-SBT bridge (com.novocode:junit-interface:0.11) - after adding the corresponding dependency (by uncommenting it in pom.xml) and reimporting the project, we get the desired result:

$ bloop test my-artifact-test
...
===============================================
Total duration: 9ms
1 failed

Failed:
- BarTest:
  * BarTest.evaluatesExpression - java.lang.AssertionError: expected:<4> but was:<5>
===============================================

Workaround

Add this dependency to your pom.xml:

    <dependency>
      <groupId>com.novocode</groupId>
      <artifactId>junit-interface</artifactId>
      <version>0.11</version>
      <scope>test</scope>
    </dependency>

Note that this is inconvenient, since it requires modifying the Maven project and preserving the change for later imports, either by pushing it upstream with the corresponding review process, or by inventing some way to "keep it alive" locally for future imports. I'm not aware of a palatable way to add a dependency to an existing Maven project without modiying pom.xml (like a local .sbt file in SBT).

Thanks for reporting! Bloop uses the same mechanism for tests as sbt and without junit-interface class it's unable to use the junit test framework. We might want to add the interface if it's missing in the project, though I am not sure if it's the perfect solution 🤔

This would need to be done around https://github.com/scalacenter/bloop/blob/master/integrations/maven-bloop/src/main/scala/bloop/integrations/maven/MojoImplementation.scala#L185