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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:12:27+00:00 2026-05-30T02:12:27+00:00

What do you do with methods that throw exceptions in jUnit tests? As you

  • 0

What do you do with methods that throw exceptions in jUnit tests? As you see the addAnswer method in the Question class may throw a exception. In the shouldFailifTwoAnswerAreCorrect method I want to check if the exception is thrown but in the shouldAddAnswersToQuestion

Should I add throws MultipleAnswersAreCorrectException from the private addAnswerToQuestion method and try/catch in the shouldAddAnswersToQuestion or throw it in that method too?

What do you do when methods throw exception(s) in tests?

public class QuestionTest {

    private Question question;

    @Before
    public void setUp() throws Exception {
        question = new Question("How many wheels are there on a car?", "car.png");
    }

    @Test
    public void shouldAddAnswersToQuestion() {

        addAnswerToQuestion(new Answer("It is 3", false));
        addAnswerToQuestion(new Answer("It is 4", true));
        addAnswerToQuestion(new Answer("It is 5", false));
        addAnswerToQuestion(new Answer("It is 6", false));

        assertEquals(4, question.getAnswers().size());
    }

    @Test(expected = MultipleAnswersAreCorrectException.class)
    public void shouldFailIfTwoAnswersAreCorrect() {

        addAnswerToQuestion(new Answer("It is 3", false));
        addAnswerToQuestion(new Answer("It is 4", true));
        addAnswerToQuestion(new Answer("It is 5", true));
        addAnswerToQuestion(new Answer("It is 6", false));
    }

    private void addAnswerToQuestion(Answer answer) {
        question.addAnswer(answer);
    }
}

Method in Question class

public void addAnswer(Answer answer) throws MultipleAnswersAreCorrectException {

    boolean correctAnswerAdded = false;

    for (Answer element : answers) {
        if (element.getCorrect()) {
            correctAnswerAdded = true;
        }
    }

    if (correctAnswerAdded) {
        throw new MultipleAnswersAreCorrectException();
    } else {
        answers.add(answer);    
    }
}
  • 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-30T02:12:28+00:00Added an answer on May 30, 2026 at 2:12 am

    You have to add throws declaration to addAnswerToQuestion and then either try/catch the exception or use expected attribute or @Test:

    @Test(expected=IOException.class)
    public void test() {
        // test that should throw IOException to succeed.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When throwing exceptions between multiple methods, should all methods re-throw the exception? for example
In Java, I have a class with a few methods that throw the same
Is there anyway to determine the Exceptions that can be thrown by methods in
I have an exception being thrown from a C# method, that takes a generic
I am trying to test that a particular method throws an expected exception from
I have two action methods that are conflicting. Basically, I want to be able
I have a test that expects a particular exception, for example: @Test(expected=MyException.class) public void
I need to write JUnit tests for an old application that's poorly designed and
A few of my methods in Java throw exceptions such as NoSuchElementException, IllegalArgumentException, etc.
What are the best practices for exceptions over remote methods? I'm sure that you

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.