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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:44:43+00:00 2026-06-07T05:44:43+00:00

I found @Rule annotation in jUnit for better handling of exception. Is there a

  • 0

I found @Rule annotation in jUnit for better handling of exception.
Is there a way to check error code ?

Currently my code looks like (without @Rule):

 @Test
    public void checkNullObject() {
    MyClass myClass= null;
    try {
        MyCustomClass.get(null); // it throws custom exception when null is passed
    } catch (CustomException e) { // error code is error.reason.null
        Assert.assertSame("error.reason.null", e.getInformationCode());
    }
    }

But with use of @Rule, I am doing following :

        @Rule
        public ExpectedException exception = ExpectedException.none();

        @Test
        public void checkNullObject() throws CustomException {
        exception.expect(CustomException .class);
        exception.expectMessage("Input object is null.");
        MyClass myClass= null;
        MyCustomClass.get(null);

        }

But, I want to do something like below:

       @Rule
        public ExpectedException exception = ExpectedException.none();

        @Test
        public void checkNullObject() throws CustomException {
        exception.expect(CustomException .class);
       //currently below line is not legal. But I need to check errorcode.
        exception.errorCode("error.reason.null");
        MyClass myClass= null;
        MyCustomClass.get(null);

        }
  • 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-07T05:44:44+00:00Added an answer on June 7, 2026 at 5:44 am

    You can use a custom matcher on the rule with the expect(Matcher<?> matcher) method.

    For example:

    public class ErrorCodeMatcher extends BaseMatcher<CustomException> {
      private final String expectedCode;
    
      public ErrorCodeMatcher(String expectedCode) {
        this.expectedCode = expectedCode;
      }
    
      @Override
      public boolean matches(Object item) {
        CustomException e = (CustomException)item;
        return expectedCode.equals(e.getInformationCode());
      }
    }
    

    and in the test:

    exception.expect(new ErrorCodeMatcher("error.reason.null"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found out that I can use 'error' in the grammar rule as
Yesterday I found something very strange (I think). It looks like Form.TransparencyKey gives different
I was looking at a css file today and found the following rule set:
FOUND THE ANSWER MYSELF, see below... I'd like to have a very simple user
found this little code snippet that seems to do what i want, but im
Found a piece of code today, that I find a little smelly... TMyObject.LoadFromFile(const filename:
I'm trying to learn more about database transactions, I found the ACID rule of
I am looking for an .htaccess rule for the following situation. I have found
Today i tested my website on seo tool site and found following error: Your
I have the following rewrite rule in my htaccess file, which I found suggested

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.