Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The distinction is rather easy:</p> <ul> <li>extending <code>TestCase</code> is the way unit tests were written in JUnit 3 (of course it's still supported in JUnit 4)</li> <li>using the <code>@Test</code> annotation is the way introduced by JUnit 4</li> </ul> <p>Generally you should choose the annotation path, unless compatibility with JUnit 3 (and/or a Java version earlier than Java 5) is needed. The new way has several advantages:</p> <ul> <li>The <a href="http://junit.org/apidocs/org/junit/Test.html" rel="noreferrer"><code>@Test</code> annotaton</a> is more explicit and is easier to support in tools (for example it's easy to search for all tests this way)</li> <li>Multiple methods can be annotated with <a href="http://junit.org/apidocs/org/junit/Before.html" rel="noreferrer"><code>@Before</code></a>/<a href="http://junit.org/apidocs/org/junit/BeforeClass.html" rel="noreferrer"><code>@BeforeClass</code></a> and <a href="http://junit.org/apidocs/org/junit/After.html" rel="noreferrer"><code>@After</code></a>/<a href="http://junit.org/apidocs/org/junit/AfterClass.html" rel="noreferrer"><code>@AfterClass</code></a> providing more flexibility</li> <li>Support for <a href="https://github.com/junit-team/junit/wiki/Rules" rel="noreferrer"><code>@Rule</code> annotations</a> on things like <a href="http://junit.org/apidocs/org/junit/rules/ExpectedException.html" rel="noreferrer"><code>ExpectedException</code></a></li> <li>Support for the <a href="http://junit.org/apidocs/org/junit/Ignore.html" rel="noreferrer"><code>@Ignored</code></a> annotation</li> <li>Support for alternative test runners using <a href="http://junit.sourceforge.net/javadoc/org/junit/runner/RunWith.html" rel="noreferrer"><code>@RunWith</code></a></li> </ul> <p>To test for expected exceptions in a JUnit 3 <code>TestCase</code> you'd have to make the text explicit.</p> <pre><code>public void testMyException() { try { objectUnderTest.myMethod(EVIL_ARGUMENT); fail("myMethod did not throw an Exception!"); } catch (MyException e) { // ok! // check for properties of exception here, if desired } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload