No Run As > "JUnit Test" when right clicking on a (closed) source containing JUnit tests
Say you have the following source in your project:
If the 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" is shown.
The problem is that if the source has not been opened, then the @Test annotation can only be found in the AST, and not in the symbol. The fix is to traverse the AST and check for @Test occurrences.
There is only one occurrence when this doesn't work, which is when a class declares no tests but it inheriths tests from a superclass. The problem here is that we cannot access the AST of the superclass if it the superclass is defined in a different compilation unit (there is no way to go from a symbol to its AST). The right way to fix this is to us the ScalaSearch component, once we will have it. I've opened another ticket for keeping track of this #1001379.
package test
import org.junit.Test
class MyTest {
@Test
def test1() {}
}
If the 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" is shown.
The problem is that if the source has not been opened, then the @Test annotation can only be found in the AST, and not in the symbol. The fix is to traverse the AST and check for @Test occurrences.
There is only one occurrence when this doesn't work, which is when a class declares no tests but it inheriths tests from a superclass. The problem here is that we cannot access the AST of the superclass if it the superclass is defined in a different compilation unit (there is no way to go from a symbol to its AST). The right way to fix this is to us the ScalaSearch component, once we will have it. I've opened another ticket for keeping track of this #1001379.
Leave a comment
on 2012-12-12 15:13 *
By Mirco Dotta
(Comment removed)
(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