Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:03:13+00:00 2026-05-26T18:03:13+00:00

How can I mark a test as an expected failure in JUnit 4? In

  • 0

How can I mark a test as an expected failure in JUnit 4?

In this case I want to continue to run this test until something is patched upstream. Ignoring the test goes a little too far, as then I might forget about it. I may be able to add an @expected annotation and catch the exception thrown by assertThat, but that also seems to lie about the expected behavior.

Here’s what my current test looks like:

@Test
public void unmarshalledDocumentHasExpectedValue() 
{
    doc = unmarshaller.unmarshal(getResourceAsStream("mydoc.xml"));
    final ST title = doc.getTitle();
    assertThat(doc.getTitle().toStringContent(), equalTo("Expected"));
}

That assert should succeed, but because of an upstream bug it doesn’t. Yet, that test is correct; it should succeed. Virtually all the alternatives that I’ve found are misleading. Right now I think @Ignore("This test should pass once fixed upstream") is my best bet, but I still have to remember to come back to it. I’d prefer that the test run.

In Python I can use the expectedFailure decorator:

class ExpectedFailureTestCase(unittest.TestCase):
    @unittest.expectedFailure
    def test_fail(self):
        self.assertEqual(1, 0, "broken")

With Qt’s QTestLib in C++, you can use QEXPECT_FAIL:

QEXPECT_FAIL("", "Will be fixed next version", Continue);
QCOMPARE(i, 42);

In both cases above, the unit test runs which is what I’m hoping to have happen. Am I missing something in JUnit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:03:13+00:00Added an answer on May 26, 2026 at 6:03 pm

    I’m assuming here that you want the test to pass if your assert fails, but if the assert succeeds, then the test should pass as well.

    The easiest way to do this is to use a TestRule. TestRule gives the opportunity to execute code before and after a test method is run. Here is an example:

    public class ExpectedFailureTest {
        public class ExpectedFailure implements TestRule {
            public Statement apply(Statement base, Description description) {
                return statement(base, description);
            }
    
            private Statement statement(final Statement base, final Description description) {
                return new Statement() {
                    @Override
                    public void evaluate() throws Throwable {
                        try {
                            base.evaluate();
                        } catch (Throwable e) {
                            if (description.getAnnotation(Deprecated.class) != null) {
                                // you can do whatever you like here.
                                System.err.println("test failed, but that's ok:");
                            } else {
                                throw e;
                            }
                        }
                    }
                };
            }
        }
    
        @Rule public ExpectedFailure expectedFailure = new ExpectedFailure();
    
        // actually fails, but we catch the exception and make the test pass.
        @Deprecated
        @Test public void testExpectedFailure() {
            Object o = null;
            o.equals("foo");
        }
    
        // fails
        @Test public void testExpectedFailure2() {
            Object o = null;
            o.equals("foo");
        }
    }
    

    First, note that the first method is marked as @Deprecated. I’m using this as a marker for the method for which I want to ignore any assertion failures. You can do whatever you like to identify the methods, this is just an example.

    Next, in the ExpectedFailure#apply(), when I do the base.evaluate(), I’m catching any Throwable (which includes AssertionError) and if the method is marked with the annotation @Deprecated, I ignore the error. You can perform whatever logic you like to decide whether you should ignore the error or not, based on version number, some text, etc. You can also pass a dynamically determined flag into ExpectedFailure to allow it to fail for certain version numbers:

    public void unmarshalledDocumentHasExpectedValue() {
        doc = unmarshaller.unmarshal(getResourceAsStream("mydoc.xml"));
    
        expectedFailure.setExpectedFailure(doc.getVersionNumber() < 3000);
    
        final ST title = doc.getTitle();
        assertThat(doc.getTitle().toStringContent(), equalTo("Expected"));
    }
    

    For further examples, see ExternalResource, and ExpectedException

    Ignoring an expected failure test rather than passing it

    If you want to mark you tests as Ignored rather than Success, it becomes a bit more complex, because tests are ignored before they are executed, so you have to retrospectively mark a test as ignored, which would involve constructing your own Runner. To give you a start, see my answer to How to define JUnit method rule in a suite?. Or ask another question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I mark a unit test in SUnit (or phexample) as expected failures?
In JUnit you can use @Ignore before methods to tell the test runner to
This is my code : function mark() { alert("This is a test box.."); }
i am currently writing a webapp in rails where users can mark items as
I like to print out software requirements so I can easily mark them up,
PLEASE CHECK ANSWERS by VolkerK too, he provided another solution, but I can't mark
How can I find the high water mark (the historical maximum number of concurrent
How can I put a question mark above a less-than-or-equal-to symbol( \leq ) in
Can I intercept a method call in Objective-C? How? Edit: Mark Powell 's answer
I'd like to mark a method as deprecated, so the people using it can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.