How to define a Junit test case without using @Test annotation

I need to run a Junit test case without using the @Test annotation. It works fine if I use the @Test annotation.

I would like to go for automation testing to do Junit Testing as the main one. Can you give me an example program for this?

Note. I tried Test Suite, so please do not give any examples regarding the test suite.

+4
source share
1 answer

You can use JUnit 3

 import junit.framework.TestCase; public class DummyTestA extends TestCase { public void testSum() { int a = 5; int b = 10; int result = a + b; assertEquals(15, result); } } 

But usually you should choose the annotation path if compatibility with JUnit 3 (and / or the Java version earlier than Java 5) is not required for the following reasons:

  • The @Test annotation is @Test and easier to maintain in tools (for example, it's easy to find all tests in this way)
  • Several methods can be annotated using @Before/@BeforeClass and @After/@AfterClass provides more flexibility.
  • Integrated support for testing expected exceptions using expected=
  • Integrated Support @Ignored annotation
  • new features of JUnit 4 : rules , parameterized tests , ...

see also JUnit website

+5
source

Source: https://habr.com/ru/post/1495448/