No Run As > "JUnit Test" when right clicking on a (closed) source containing a class that inherits JUnit tests
Say you have the following sources in your project (in separate compilation units):
Further, make sure both sources are closed (i.e., the haven't been typechecked yet - this is important!) and, through the package explorer, you right click on "MyTest.scala" and select Run As.
Well, no "JUnit Test" option is shown!
The reason why this doesn't work is the following:
While traversing "MyTest.scala", no @Test annotation is found, so we need to look in the inherited classes. We can do so by retrieving the superclass' symbol. However, because "CommonTest.scala has not been opened yet, the @Test annotation can only be found in the AST, and not in the symbol. Therefore, checking the superclass' symbol won't help, and we have no way to map a symbol to its AST. The proper fix in this case would be to delegate to the ScalaSearch engine and query it to know if a @Test annotation is defined in "MyTest.scala" or any of it's superclasses.
This is a good use case for the ScalaSearch, so let's fix this once we have it ;-)
// test/CommonTest.scala
package test
import org.junit.Test
abstract class CommonTest {
@Test
def test1() {}
}
// test/MyTest.scala
package test
class MyTest extends CommonTest
Further, make sure both sources are closed (i.e., the haven't been typechecked yet - this is important!) and, through the package explorer, you right click on "MyTest.scala" and select Run As.
Well, no "JUnit Test" option is shown!
The reason why this doesn't work is the following:
While traversing "MyTest.scala", no @Test annotation is found, so we need to look in the inherited classes. We can do so by retrieving the superclass' symbol. However, because "CommonTest.scala has not been opened yet, the @Test annotation can only be found in the AST, and not in the symbol. Therefore, checking the superclass' symbol won't help, and we have no way to map a symbol to its AST. The proper fix in this case would be to delegate to the ScalaSearch engine and query it to know if a @Test annotation is defined in "MyTest.scala" or any of it's superclasses.
This is a good use case for the ScalaSearch, so let's fix this once we have it ;-)
Leave a comment
on 2012-12-12 15:14 *
By Mirco Dotta
It turns out that this if one calls `member.initialize` before `member.hasAnnotation(ann)`, then the @Test annotation is going to be correctly propagated! This implies: no need to use ScalaSearch!
on 2012-12-13 15:52 *
By Mirco Dotta
Assigned to set to Mirco Dotta
Status changed from New to Fixed
(In scala-ide:1891f62e2ce92f15591b4e9a5472b392a6c8f1d7) Show Run As > JUnit Test when right clicking on a source that contains runnable
JUnit Test classes
If a source is closed (i.e., it hasn't been typechecked yet - this is
important!) and, through the package explorer, you right click and select Run
As, no "JUnit Test" option used to be shown.
The problem was that if the source is not opened (and typechecked), the
``@Test`` annotation can only be found in the AST, and not in the symbol. The
fix is to always initialize the member's symbol before looking if it declares
the ``@Test`` annotation. By doing so, the ``@Test`` annotation is found also
for members inherited from a superclass or a trait!
In this commit I've also implemented the missing logic for considering the
JUnit4 ``@RunWith`` annotation. Basically, if a class or any of its inherited
types specify the ``@RunWith`` annotation, then the class is a valid,
runnable, JUnit4 test class.
As part of this commit I've also made some improvements to the existing testing
class (formerly named ``LaunchableTesters``, now splitted into two distinct
classes: ``MainMethodFinderTest`` and ``RunAsTest``).
The work on these tickets (Re #1001379, Re #1001380) was somewhat motivated by
(or a consequence of) Re #1001304 and Re #1001275.
Finally, I'm quite convinced that the ``ScalaLaunchShortcut.getMainMethods``
could use some similar love, but this should be done in a separate PR(there is
already enough meat here).
Fix #1001234, Fix #1001379, Fix #1001380 and Fix #1000731.
Branch: master
JUnit Test classes
If a source is closed (i.e., it hasn't been typechecked yet - this is
important!) and, through the package explorer, you right click and select Run
As, no "JUnit Test" option used to be shown.
The problem was that if the source is not opened (and typechecked), the
``@Test`` annotation can only be found in the AST, and not in the symbol. The
fix is to always initialize the member's symbol before looking if it declares
the ``@Test`` annotation. By doing so, the ``@Test`` annotation is found also
for members inherited from a superclass or a trait!
In this commit I've also implemented the missing logic for considering the
JUnit4 ``@RunWith`` annotation. Basically, if a class or any of its inherited
types specify the ``@RunWith`` annotation, then the class is a valid,
runnable, JUnit4 test class.
As part of this commit I've also made some improvements to the existing testing
class (formerly named ``LaunchableTesters``, now splitted into two distinct
classes: ``MainMethodFinderTest`` and ``RunAsTest``).
The work on these tickets (Re #1001379, Re #1001380) was somewhat motivated by
(or a consequence of) Re #1001304 and Re #1001275.
Finally, I'm quite convinced that the ``ScalaLaunchShortcut.getMainMethods``
could use some similar love, but this should be done in a separate PR(there is
already enough meat here).
Fix #1001234, Fix #1001379, Fix #1001380 and Fix #1000731.
Branch: master