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 7884799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:53:55+00:00 2026-06-03T04:53:55+00:00

I have a method that during a specific point of processing it expects that

  • 0

I have a method that during a specific point of processing it expects that certain invariants are kept.
To keep this trivial let’s say that at point X during the code processing the variable high and the variable low MUST be divisible.
So in the code I do:

if(high % low != 0){  
  throw new AssertionError("Invalid state of low and high [low = "+ low+ ", high=" + high+"]");   
}   

During unit testing with JUnits I had a test case to test for this.
So I did:

try {
//At this point I know for sure that the low and high are not divible so process() should throw an Assertion Error
   process();  
   fail();  
} 
catch(AssertionError e){        
}

But the test case was green!
But I realised that junit raises an assert error due to fail but I catch it and as a result the test case is pass instead of failed.
From my point of view the proper error to raise in my code is also AssertionError and not some generic e.g. IllegalArgumentsException
So is there a way to fix this so that the test case works or should I not be using the AssertionError in my code in the first place? If this is the case, what exception should I be raising?

  • 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-03T04:53:56+00:00Added an answer on June 3, 2026 at 4:53 am
    boolean shouldFail = false;
    try {
        callTheMethod();
        shouldFail = true;
    }
    catch (AssertionError e) {
        // expected
    }
    if (shouldFail) {
        fail();
    }
    

    But if the condition is an invariant, then it should always be true, and the AssertionError should thus never be thrown, and you should thus not even be able to unit test it. If you’re able to test it, it means that the invariant is not really an invariant, and that the condition might be true depending on the sequence of calls or on the arguments provided. You should thus prefer an IllegalStateExcetion over the AssertionError.

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

Sidebar

Related Questions

I have this method that changes an larger image source on click. I need
I have a method that looks like this: public static String escape(String text) {
I have this method Verify_X which is called during databind for a listbox selected
I have a method that accepts an IEnumerable-decimals and performance various math functions. I
I have a method that takes a list of entities ( Class es) and
I have a method that returns a list of type string. I want to
I have a method that accepts a sender and is called with a UIbutton.
I have a method that takes an array of queries, and I need to
I have one method that opens a file and passes off to another function
I have a method that will receive a string , but before I 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.