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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:16:31+00:00 2026-06-17T14:16:31+00:00

I am writing some unit tests for a relatively simple class: public class Asylum

  • 0

I am writing some unit tests for a relatively simple class:

public class Asylum {
    List<Guard> guards = new ArrayList<Guard>();
    List<Inmate> inmates = new ArrayList<Inmate>();

    public void addGuard(Guard g) { //... }
    public void addInmate(Inmate i) { //... }
}

My unit tests for these two functions determine what happens if I pass in a null value:

@Test
public void addGuardNullValue() {
    asylum.addGuard(null);
    assertEquals(0, asylum.getNumGuards());
}

@Test
public void addInmateNullValue() {
    asylum.addInmate(null);
    assertEquals(0, asylum.getNumInmates());
}

I actually have a few (> 3) of these containers of guards, inmates, wardens, etc. but I would rather not write what is basically the same unit test for all of their classes.

Is there any way I can boil this down to one unit test to cover n classes?

(Please note, I am unable to create an overarching parent class for Guard and Inmate, and I am unable to create an Interface that they both can implement.)

  • 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-17T14:16:33+00:00Added an answer on June 17, 2026 at 2:16 pm

    I see only two solutions, and I wouldn’t recommend either.

    The first is to use reflection. List all methods in the class and select those with names like add* or getNum*. Then you could call the add* one with a null value and check the return value of getNum*. The problem is that you can have false positives in the method names (addGroup) and the resulting test would be hard to read (and write).

    The second solution would be a method addPerson(PersonType type, Object person) which downcasts person depending on PersonType. I find this a terrible option because it removes the static type check, adds unnecessary complexity and is prone to further abuse. I mention because you may find some useful variation.

    My final answer is “don’t do it, it’s not worth it”. You’ll need more lines, but they are extremely easy to read and write and will probably be bug-free at the first try.

    I would like to end with a quote I find fitting:

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it. (Brian Kernighan)

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

Sidebar

Related Questions

I have some pyunit unit tests for a simple command line programme I'm writing.
I'm writing some unit tests for my View Model class. The constructor of this
I am writing some unit tests which I have simplified to the following: class
I am writing some unit tests like the following: def executing_a_signal a_method(a_signal.new, a_model, a_helper);
I am currently writing some unit tests for a business-logic class that includes validation
I'm writing some unit tests and I have a lot of functions of the
I'm writing some unit tests for a Python library and would like certain warnings
I am writing some unit tests for an extension method I have written on
I'm writing some unit tests in UnitTest++ and want to write a bunch of
I'm writing some unit tests (PHPUnit 3.6) for my controllers and want to verify

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.