Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit @Test expected annotation not working

People also ask

What is @test annotation in JUnit?

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure.

What is @test expected?

Example@Test(expected=IllegalArgumentException.class)By using “expected” parameter, you can specify the exception name our test may throw. In above example, you are using “IllegalArgumentException” which will be thrown by the test if a developer uses an argument which is not permitted.

Which of the annotation is not available in JUnit?

Explanation: Auto, Table, Identity and Sequence are the ID generating strategies using @GeneratedValue annotation. 7. Which one of the following is not an annotation used by Junit with Junit4? Explanation: @Test, @Before, @BeforeClass, @After, @AfterClass and @Ignores are the annotations used by Junit with Junit4.

What is the use of @test in JUnit?

A JUnit test is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation. This method executes the code under test.


The problem was, that the class in which the test was nested was an extension of TestCase. Since this is JUnit 3 style, the annotation didn't work.

Now my test class is a class on its own.


@RunWith(JUnit4.class)
public class MyTestCaseBase extends TestCase 

I also had problems with @Test(expected = ...) annotation when I extended TestCase class in my base test. Using @RunWith(JUnit4.class) helped instantly (not an extremely elegant solution, I admit)


i tried this one, and work perfectly as expected.

public class SampleClassTest {
    @Test(expected = ArithmeticException.class )
    public void lost(){
        this.lost(0);
    }
    private void lost(int i) throws ArithmeticException {
        System.out.println(3/i);
    }
}

also ensure that junit4 is added as dependancy, @ (annotations) are new feature in junit 4.


I faced same issue, solution is simple "Don't extends TestCase class"


No, this JUnit test should work as it is - there is nothing more needed on this side.

What makes you sure that the test throws an IllegalStateException? Is it possible that it gets wrapped into another exception of different type?

Please post the exact failure message from JUnit.

As @duffymo suggested, it is easy to verify what (if any) exception the test really throws.


This looks correct to me.

Check your assumptions. Are you sure it throws the exception? If what you say is true, removing the expected from the annotation should make it fail.

I'd be stepping through the code with a debugger to see what's going on. I'll assume you have an IDE that will do so, like IntelliJ.