-1

I was wondering why it is necessary to put @Test above your test methods and what its use is.

Thanks in advance!

Tom
  • 279
  • 1
  • 11
  • 1
    It's necessary for JUnit to test. Please see [JUnit Cookbook](http://junit.sourceforge.net/doc/cookbook/cookbook.htm) – Frontear Jun 09 '19 at 23:25
  • It tells JUnit which methods are unit tests to be executed in the test harness. – David Jun 09 '19 at 23:26

1 Answers1

2

In order to run your unit tests, you have to tell JUnit what the tests are. Usually, you have a lot of tests, so individually listing classes/methods in a configuration file somewhere would become a major headache.

So what the framework does instead is scan all your code for classes that look like tests (using reflection). The old way was to have a defined super-class extends Test and methods following a naming pattern. Now that we have annotations, you use the @Test annotation for that.

Thilo
  • 257,207
  • 101
  • 511
  • 656