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

  • Home
  • SEARCH
  • 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 8788837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:14:13+00:00 2026-06-13T22:14:13+00:00

I want assertion failures throughout the code to cause my tests to fail. I

  • 0

I want assertion failures throughout the code to cause my tests to fail.

I have a JUnit rule like this:

public class AcmeTestRule implements TestRule {
    @Override
    public Statement apply(final Statement statement, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                ClassLoader classLoader = getClass().getClassLoader();
                classLoader.clearAssertionStatus();
                classLoader.setDefaultAssertionStatus(true);
                //classLoader.setPackageAssertionStatus("com.acme", true); // no effect

                statement.evaluate();
            }
        }
    }
}

And it is on a base test class like this:

public abstract class AcmeTestCase {
    @ClassRule
    public static final AcmeTestRule acmeTestRule = new AcmeTestRule();
}

Then to confirm that the rule itself is working, I have the following test:

public class TestAcmeTestRule extends AcmeTestCase4 {
    @Test
    public void testAssertions() {
        try {
            assert false;
        } catch (AssertionError) {
            // good.
            return;
        }

        fail("Didn't throw AssertionError on assert false");
    }
}

The test fails on our automated build because “assert false” still didn’t throw the exception. The exact same test passes run from the IDE. We’re not setting any command-line flag to turn on assertions (although the IDE might be doing it behind our back.)

Incidentally, if I do this:

public class TestAcmeTestRule {
    @ClassRule
    public static final AcmeTestRule acmeTestRule = new AcmeTestRule();

    @Test
    public void testAssertions() {
        try {
            assert false;
        } catch (AssertionError) {
            // good.
            return;
        }

        fail("Didn't throw AssertionError on assert false");
    }
}

The resulting test now fails in the IDE as well. And all I did was to move the rule from the abstract class down into the test class.

I take it this is some bizarre class loading issue. My theory: assertion status is only used when reading in the bytecode, so by the time you’re running the bytecode, it’s too late to change the value.

  • 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-06-13T22:14:14+00:00Added an answer on June 13, 2026 at 10:14 pm

    The problem with ClassLoader.setDefaultAssertionStatus (and all the similar methods) is, that they have no effect on classes which are already loaded. From the documentation

    This setting determines whether classes loaded by this class loader and initialized in the future will have assertions enabled or disabled by default.

    So you can’t reliably enable assertions in the code as long as you don’t make sure that this is executed before all other classes are loaded (which is probably error prone).

    The reason why this is working in the IDE is probably that IDEs tend to enable assertions by passing the appropriate command-line parameter, or that perhaps the order of class loading is different.

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

Sidebar

Related Questions

Say I have the following code: public class ClassToTest { AnotherClass anotherClass; public void
I want to recover all the links from a page, while executing this code
how can I use JUnit to not terminate on assertion failures, but just log
I have an error message returned for which I want to use xpath assertion
Want to run javascript function from parent window in child window Example I have
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a
Want to code a key pad for an calculator. What I want to make
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
I want to calculate users with accumulating by dates. I have the following map

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.