JUnit expected tag not working as expected

21,772

Solution 1

The problem is that your AnnounceThreadTest extends TestCase. Because it extends TestCase, the JUnit Runner is treating it as a JUnit 3.8 test, and the test is running because it starts with the word test, hiding the fact that the @Test annotiation is in fact not being used at all.

To fix this, remove the "extends TestCase" from the class definition.

Solution 2

Instead of removing extends TestCase , you can add this to run your test case with Junit4 which supports annotation.

@RunWith(JUnit4.class)

Solution 3

Just ran this in IntelliJ using JUnit 4.4:

   @Test(expected = IllegalArgumentException.class)
   public void testExpected()
   {
       throw new IllegalArgumentException();
   }

Passes perfectly.

Rebuild your entire project and try again. There's something else that you're doing wrong. JUnit 4.4 is working as advertised.

Share:
21,772

Related videos on Youtube

Ben S
Author by

Ben S

Mobile Software Engineering Manager at Square currently working on Cash App iOS with a University of Waterloo bachelor's degree in Computer Science, Software Engineering Option. Previous experience at Google, Amazon.com, OpenText, Research In Motion, Sybase and Bridgewater Systems.

Updated on March 31, 2020

Comments

  • Ben S
    Ben S about 4 years

    I have the following test case in eclipse, using JUnit 4 which is refusing to pass. What could be wrong?

    @Test(expected = IllegalArgumentException.class)
    public void testIAE() {
        throw new IllegalArgumentException();
    }
    

    This exact testcase came about when trying to test my own code with the expected tag didn't work. I wanted to see if JUnit would pass the most basic test. It didn't.

    I've also tested with custom exceptions as expected without luck.

    Screenshot: Screenshot

    • Torandi
      Torandi almost 15 years
      This one is really weird, did some testing myself, and this code runs fine (the test is successfull)...
    • Ben S
      Ben S almost 15 years
      I added a screenshot, just to show... I'd be doubtful too.
  • Ben S
    Ben S almost 15 years
    Thank you, this fixed it as advertised.
  • burtlo
    burtlo almost 15 years
    After removing the extends TestCase, I had to add the additional import to ensure I had the static assert methods. import static org.junit.Assert.*;
  • matt b
    matt b about 14 years
    Awesome job at finding the solution hidden as a hint in a screenshot
  • Patelify
    Patelify over 12 years
    Solved!! Thank you, saved me some time.
  • Dylan Knowles
    Dylan Knowles over 9 years
    Argggghhhh, I just spent an hour and a half on this issue without finding this answer. Thank you, thank you, thank you!
  • Seansms
    Seansms over 7 years
    The screen shot is a broken link now so we can't see what the original class looked like. I found that if I renamed my class and my tests to no longer start with "test" that suddenly @Begin works and "expected" works too. Thanks Yishai!
  • Yishai
    Yishai over 7 years
    @Seansms, the original screen shot shows that the class extended TestCase. I assume that is your problem as well. Don't extend TestCase, that is the JUnit 3.8 way.