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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:24:36+00:00 2026-06-13T11:24:36+00:00

In JUnit 4 you can declare expected exception using @Test(expected = SomeException.class) annotation. However,

  • 0

In JUnit 4 you can declare expected exception using @Test(expected = SomeException.class) annotation. However, when testing is done using Theories, @Theory annotation does not have expected property.

What is the best way to declare expected exception when testing Theories?

  • 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-13T11:24:37+00:00Added an answer on June 13, 2026 at 11:24 am

    I prefer using ExpectedException rule:

    import org.junit.rules.ExpectedException;
    
    <...>
    
    @Rule
    public ExpectedException thrown = ExpectedException.none();
    
    @Theory
    public void throwExceptionIfArgumentIsIllegal(Type type) throws Exception {
        assumeThat(type, equalTo(ILLEGAL));
        thrown.expect(IllegalArgumentException.class);
        //perform actions
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I have a basic question: When using JUnit, in your test suite (the class
How can I mark a test as an expected failure in JUnit 4? In
How can I use JUnit idiomatically to test that some code throws an exception?
Can I have more than one method with @Parameters in junit test class which
Can we have Junit Test cases for testing Proxy, Business Services in Oracle Service
Using the JUnit assumeTrue, you can skip a test, but is there any way
Hello I'm writting junit test how can I test this method .. this is
Essence: How can I auto-rollback my hibernate transaction in a JUnit Test run with
junit.framework.AssertionFailedError: Class com.android.deviceintelligence.test.Testshutdown has no public constructor TestCase(String name) or TestCase() at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at

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.