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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:25:38+00:00 2026-05-15T19:25:38+00:00

We have a bunch of classes that listen for events from the server and

  • 0

We have a bunch of classes that listen for events from the server and then respond to them. For example:

class EventManager {
    private Set<Event> cache = new HashSet<Event>();

    private EventListener eventListener = new EventListener() {
        void onEvent(Event e) {
          if (e instanceof MyEvent || e instanceof YourEvent) {
            handleEvent(e);
          }  
        }
    }

    public EventManager(ServerCommunication serverComm) {
        serverComm.addListener(eventListener);
    }

    private handleEvent(Event e) {
        // handle the event...
        // ...
        cache.add(cache);
        // ...
    }
}

Here’s a made-up example of the kind of thing we are doing. Here are the problems I see:

  1. I’d like to test handleEvent to make sure it’s doing what it is supposed to but I can’t because it’s private.
  2. I’d also like to check that something got added to the cache too but that also seems difficult since cache is a private member and I don’t want to add a needless getter method.
  3. I’d also like to test the code inside the anonymous class’s onEvent method.

For now, what I did was move all logic from the anonymous class to the handleEvent method, and I made handleEvent package private (my unit test is in the same package). I’m not checking the contents of the cache although I want to.

Does anyone have any suggestion for a better design that is more testable?

  • 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-15T19:25:39+00:00Added an answer on May 15, 2026 at 7:25 pm

    I would probably extract a EventCache component. You can replace this for your test with an implementation that counts the cached events or records whatever is of interest.

    I probably would not change the visibility of handleEvent. You could implement a ServerCommunication that just raises the event from the test case.

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

Sidebar

Related Questions

I have a whole bunch of child classes that inherit from a parent class
Let's say I have a bunch of classes that look something like... class Foo{
I have a bunch of classes that each read in their values from an
I have a bunch of stored, serialized classes (that all inherit from a base
I have a bunch of classes in a C# class library that I bought
I have a base class and several concrete classes that derive from it. Lets
I have a class, which has a bunch of subclasses that inherit from it.
I have a bunch of third-party Java classes that use different property names for
In my program, I've got a bunch of classes that will only ever have
I have a web application that contains a bunch of classes in the App_Code

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.