scala-js/scala-2.12-junit-mixin-plugin

Name: scala-2.12-junit-mixin-plugin

Owner: Scala.js

Description: Restore your JUnit @Test methods in traits with Scala 2.12.0-M5

Created: 2016-06-06 15:02:35.0

Updated: 2018-03-29 09:22:22.0

Pushed: 2016-09-21 19:57:04.0

Homepage:

Size: 15

Language: Scala

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Notice of obsolescence

As of Scala 2.12.0-RC1, this plugin is obsolete, thanks to scala/scala#5369.

scala-2.12-junit-mixin: restore your JUnit @Test methods in traits

Build Status

With Scala 2.12.0-M5 and JUnit 4, you might have encountered warnings such as the following:

[warn] TraitToClassMixinTest.scala:24: JUnit tests in traits that are
       compiled as default methods are not executed by JUnit 4.
       JUnit 5 will fix this issue.
[warn]   @Test def test2(): Unit = {
[warn]             ^

Uh oh! Some of your test suite just doesn't run anymore! As the warning tells you, this will be fixed if you upgrade to JUnit 5 … except that is not released yet.

scala-2.12-junit-mixin solves this issue right now, with Scala 2.12.0-M5 and JUnit 4. Simply add the following to your project settings:

he `test-plugin` configuration adds a plugin only to the `test`
onfiguration. It is a refinement of the `plugin` configuration which adds
t to both `compile` and `test`.

onfigurations += config("test-plugin").hide
acOptions in Test ++= {
l report = update.value
l jars = report.select(configurationFilter("test-plugin"))
r {
jar <- jars
jarPath = jar.getPath
// This is a hack to filter out the dependencies of the plugins
if jarPath.contains("plugin")
yield {
s"-Xplugin:$jarPath"



dd the scala-junit-mixin-plugin to the test configuration when on 2.12
aryDependencies ++= {
 (scalaVersion.value.startsWith("2.10.") || scalaVersion.value.startsWith("2.11."))
Seq.empty
se
Seq("org.scala-js" % "scala-junit-mixin-plugin" % "0.1.0" % "test-plugin" cross CrossVersion.full)

scala-2.12-junit-mixin-plugin is platform-independent. It works with Scala on the JVM and with Scala.js.

How it works

The compiler plugin performs a very simple job. When a JUnit-related method is inherited by a class from an interface as a default method, an explicit forwarder is added in the class to call the default method.

For example, consider the following Scala code:

t MyTestsBase {
est def add(): Unit =
assertEquals(3, 1 + 2)

f notATest(): Unit =
assertEquals(5, 2 + 3)


s MyTests extends MyTestsBase

Scala 2.12.0-M5 identifies that add() does not need a forward in MyTests, because it will be inherited as a default method on the JVM. But this fools JUnit 4, which does not look for @Test methods in interfaces. In this case, the plugin rewrites MyTests as follows:

s MyTests extends MyTestsBase {
est override def add(): Unit =
super[MyTestsBase].add()

This allows JUnit to find MyTests.add(). Note that notATest() does not receive the plugin treatment, because it is not JUnit-related.

JUnit-related methods are those annotated with one of the following annotations: @Test, @Before, @After, @BeforeClass, @AfterClass, @Ignore.

License

scala-2.12-junit-mixin-plugin is distributed under the BSD 3-Clause license.

Contributing

Follow the contributing guide.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.