I have a project in Java with JUnit tests in Scala. Each test class is annotated with @Test:
import org.junit.Test
@Test
class SomeTest {
...
}
JUnit API says that @Test is a method annotation. However, when I delete the @Test annotations from the classes while keeping method annotations intact, many tests are not executed when running from Eclipse.
So what is the purpose of @Test annotation applied to a class and why are some of the tests not run when these annotations removed?
Scala does not check the
ElementTypefor annotations (even outside of Scala IDE), so there is no ‘purpose’ for a @Test annotation for a class in Scala.In fact, you can apply any annotation to a class, Scala does not prevent this. Using the following example:
after compilation, you get a @Rule annotation on the class. This is a known feature, because AFAIK, you can ‘apply’ an annotation on a field in the source, but will actually end up on the method, if you use on of the target annotations.
If you’re running your tests through any of the standard test runners, the
@Teston the class shouldn’t make any difference. The only thing that counts is the@Testannotation on the method.When you’re using JUnit in Scala, you should follow the same rules as with Java.