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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:34:44+00:00 2026-05-15T08:34:44+00:00

Edit: Not JUnit 4 available at this time. Hi there, I have a question

  • 0

Edit: Not JUnit 4 available at this time.

Hi there,

I have a question about “smart” exception testing with JUnit. At this time, I do it like this:

public void testGet() {

    SoundFileManager sfm = new SoundFileManager();

        // Test adding a sound file and then getting it by id and name.
        try {
            SoundFile addedFile = sfm.addSoundfile("E:\\Eclipse_Prj\\pSound\\data\\Adrenaline01.wav");
            SoundFile sf = sfm.getSoundfile(addedFile.getID());
            assertTrue(sf!=null);
            System.out.println(sf.toString());

            sf = sfm.getSoundfileByName("E:\\Eclipse_Prj\\pSound\\data\\Adrenaline01.wav");
            assertTrue(sf!=null);
            System.out.println(sf.toString());
        } catch (RapsManagerException e) {
            System.out.println(e.getMessage());
        }

        // Test get with invalid id. 
        try {
            sfm.getSoundfile(-100);
            fail("Should have raised a RapsManagerException");
        } catch (RapsManagerException e) {
            System.out.println(e.getMessage());
        }

        // Test get by name with invalid name
        try {
            sfm.getSoundfileByName(new String());
            fail("Should have raised a RapsManagerException");
        } catch (RapsManagerException e) {
            System.out.println(e.getMessage());
        }

    }

As you can see, I need one try/catch block for each function that is supposed to throw an exception. It seems not to be a good way to do this – or is there no possibility to reduce the use of try/catch?

  • 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-15T08:34:45+00:00Added an answer on May 15, 2026 at 8:34 am

    I suggest that you need to break up testGet into multiple separate tests. The individual try/catch blocks seem to be pretty independent of each other. You may also want to extract the common initialization logic into its own setup method.

    Once you have that, you can use JUnit4’s exception annotation support, something like this:

    public class MyTest {
    
    private SoundManager sfm;
    
    @Before
    public void setup() {
          sfm = new SoundFileManager();
    }
    
    @Test
    public void getByIdAndName() {
      // Test adding a sound file and then getting it by id and name.
          SoundFile addedFile =              
          sfm.addSoundfile("E:\\Eclipse_Prj\\pSound\\data\\Adrenaline01.wav");
          SoundFile sf = sfm.getSoundfile(addedFile.getID());
          assertTrue(sf!=null);
          System.out.println(sf.toString());
    
          sf = sfm.getSoundfileByName("E:\\Eclipse_Prj\\pSound\\data\\Adrenaline01.wav");
          assertTrue(sf!=null);
          System.out.println(sf.toString());
    }
    
    @Test(expected=RapsManagerException.class)
    public void getByInvalidId() {
          // Test get with invalid id. 
          sfm.getSoundfile(-100);
    }
    
    @Test(expected=RapsManagerException.class)
    public void getByInvalidName() {
          // Test get with invalid id. 
          sfm.getSoundfileByName(new String());
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a design question. Suppose you have a method like this
[edit] I am NOT using jquery in this app. Looking for a way to
edit: I'm not looking for you to debug this code. If you are familiar
This is quite similar question to one older but the solution did not work
hI, Is there any control that prints flow document content to PDF? EDIT: not
EDIT: It is not a listbox. My mistake. It is a list view. I
Edit: It's not a bug as Martin pointed out. I'm just crossing the daylight
[EDIT] I am NOT accepting any answer which involves BigInteger, or other similarly inefficient
LATER EDIT: Please mention not only game programming books, but also more scientific/simulation oriented
EDIT I isolated a real minimal example which does not work (it is a

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.