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

The Archive Base Latest Questions

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

Suppose I have a method named foo which, for certain set of input values,

  • 0

Suppose I have a method named foo which, for certain set of input values, is expected to complete successfully and return a result, and for some other set of values, is expected to throw a certain exception. This method requires some things to have set up before it can be tested.

Given these conditions, is it better to club success and failure tests in one test, or should I maintain these cases in separate test methods?

In other words, which of the following two approaches is preferable?

Approach 1:

@Test
public void testFoo() {
  setUpThings();
  // testing success case
  assertEquals(foo(s), y);
  // testing failure case
  try {
    foo(f);
    fail("Expected an exception.")
  } catch (FooException ex) {
  }
}

Approach 2:

@Test
public void testFooSuccess() {
  setUpThings();
  assertEquals(foo(s), y);
}

@Test
public void testFooFailure() {
  setUpThings();
  try {
    foo(f);
    fail("Expected an exception.")
  } catch (FooException ex) {
  }
}
  • 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-13T21:51:37+00:00Added an answer on June 13, 2026 at 9:51 pm

    Best you go for approach #2.

    Why:

    Well when an asserts fails the rest of the method is not evaluated…
    so by putting the tests in 2 separate methods you are sure to at least execute both tests.. failure or not.

    Not only should a unit test focus on one specific unit, it should focus on one specific behaviour of that unit. Testing multiple behaviours at once only muddies the water.
    Take the time to separate each behaviour into its own unit test.

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

Sidebar

Related Questions

Suppose, I have a controller named category with an action method, Index which takes
Suppose I have a method which changes the state of an object, and fires
Suppose I have a method that must return an object (say from a database
Suppose I have the following method signature int f (int[] values) and I call
Suppose I have a form : <form id=myForm method=POST action=/something/somewhere> <input type=text name=textField />
Suppose I have two methods bool Foo() and bool Bar() . Which of the
Suppose we have a class named Calculator . There's a class method in it,
Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms
suppose I have a model and a view ,ths view have two method:one is
Suppose I have a static method of my class that returns an object of

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.