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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:06:56+00:00 2026-06-07T18:06:56+00:00

I am writing unit tests for our main product and have one concern: how

  • 0

I am writing unit tests for our main product and have one concern: how to differentiate

  • tests that failed because what they tested went wrong (a bug was found and its a non regression test for it for instance)
  • tests that failed because another unexpected part of the test failed (because the test is wrong or an unknown bug appeared)

For the first one, we have the jUnit Assert framework for sure, but what do we have for the second one?

Example: my unit test is testing that c() will not throw MyException, but to perform c() I need to first perform a() then b() which both can throw MyException(), so I would write:

@Test
public void testC() {
  a();
  Object forC = b();
  try {
    c(forC);
  } catch (MyException e) {
    Assert.fail("....");
  }
}

But then I need to handle the MyException that can be thrown by a or b, and also handle the fact that forC should not be null. What is the best way to do this?

  • Catch MyException thrown by a or b and Assert.fail, but a and b are not tested by this test so for me they shouldn’t be marked as test failure when they fail. Maybe they fail later because at this time we should do b();a() not a();b();.
  • Let testC throws MyException, so that the test will fail with “MyException”, but this is misleading because MyException will not tell that the test is wrongly written. All tests will then fail each with their own Exception. In this case I would also need to throw something like NullPointerException if forC is null, which also has no semantic.
  • Catch and wrap MyException thrown by a and b into a exception telling that the test might be wrong, like a TestCorruptedException. But I couldnt find such exceptions in jUnit, so they would not be recognized by jUnit as such (that’s ok for me). Also I would need to know this exception from all my unit tests which are of course split into multiple modules, projects and so on…So this is doable but adds dependencies.

What would be your solution to this problem? I will likely go for the second but I’m not pleased with it as explained above.

  • 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-07T18:06:58+00:00Added an answer on June 7, 2026 at 6:06 pm

    The corner stone of unit testing is to test the minimum amount of code necessary, otherwise it arguably falls into the integration test space where you are looking for end-to-end functionality.

    If you can prove that a() can be created and tested in it’s own test class, and that b() can also be created and tested in it’s own test class, then it follows that your test above can ignore the testing of a() and b() and instead use known values that won’t fail. This is commonly satisfied by using mock objects.

    With a() and b() created as mock objects, c() can be tested in isolation. If you find that it is not possible to test c() in isolation of a() and b() then this is an indicator that your code needs to change so that there is a separation of concerns. This is commonly satisfied by injecting the dependany of a() and b() into c().

    This post on when to use mock objects in unit testing may help shed some more light on this topic.

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

Sidebar

Related Questions

We have all of our unit tests written so that they create and populate
We are writing unit tests for our ASP.NET application that run against a test
I'm writing some unit tests to assert that all our controller action methods are
I have a PersonDao that I'm writing unit tests against. There are about 18-20
I'm writing some unit tests that serialize and deserialize all of our types that
I have been writing unit tests using NUnit and Moq with my Silverlight code
I’ve been writing unit tests in strongly typed languages and I have a fair
I'm writing some unit tests and I have a lot of functions of the
We have an old asp.net application that has no unit tests, integration tests, component
When writing unit tests, there are cases where one can create an Assert for

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.