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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:47:39+00:00 2026-06-04T02:47:39+00:00

There is an interface: interface IEventListener { void onEvent(List <IEvent> events); } There is

  • 0

There is an interface:

interface IEventListener
{
  void onEvent(List <IEvent> events);
}

There is an event class:

class EventB
{
  private final int id;
  private final A a;
  private final String somethingElse;
...
// constructors, getters
...
}

And there is a class to test:

class Doer
{
 IEventListener eventListener;
 void doSomething(Aaa a)
 {
   eventListener.onEvent(Arrays.asList(new EventA(1), new EventC(2)));
...
   eventListener.onEvent(Arrays.asList(new EventB(42, a.getA(), "foo"), new EventA(3), new EventB(0, a.getA(), "bar")));
...
   eventListener.onEvent(Arrays.asList(new EventC(4)));
 }
}

The Doer is a code which I need to test, the method doSomething produces packs of events, and I need to test if it produces a particular event in some specific conditions.

More precisely I want to have a unit test which calls the method doSomething and checks that EventB is sent with “42” and A as from method argument a. All other events should be ignored.

To make such test I’ve only came up with solution involving quite verbose code with ArgumentCaptor, for-blocks, and magic boolean flags…

What is the best way to make a unit test for it? Maybe the code design is bad?

  • 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-04T02:47:40+00:00Added an answer on June 4, 2026 at 2:47 am

    The design is correct, this is how you test it with Mockito:

    import org.hamcrest.Matchers;
    import org.mockito.Mockito;
    public void firesEventsOnDoSomething() {
      Listener listener = Mockito.mock(Listener.class);
      Doer doer = new Doer(listener);
      doer.doSomething(aaa);
      Mockito.verify(listener).onEvent(
        Mockito.argThat(
          Matchers.hasItem(
            Matchers.allOf(
              Matchers.instanceOf(EventB.class),
              Matchers.hasProperty("a", Matchers.equalTo(aaa.getA())),
              // whatever you want
            )
          )
        )
      );
    }
    

    It’s Mockito 1.9.0 and Hamcrest-library 1.2.1.

    To use JUnit 4.10 together with Hamcrest-library 1.2.1 you should use junit:junit-dep:4.10 artifact, and exclude org.hamcrest:hamcrest-core from it:

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit-dep</artifactId>
      <version>4.10</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.2.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-library</artifactId>
      <version>1.2.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.9.0</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Event dispatcher interface public interface EventDispatcher { <T> EventListener<T> addEventListener(EventListener<T> l); <T> void removeEventListener(EventListener<T>
There exists interface Algorithm There exists class MathAlgorythm implements Algorithm (returns MathResult, which implements
Is there public interface in SDK 3.* to start video recording programmatically? Also how
Is there any Fluent WCF interface out there worth using? Maybe something like these:
Is there a common interface in Python that I could derive from to modify
I've already known that there is an interface in UIWebView for object c to
There are a couple of data providers to interface with MySQL databases from .NET
Is there a simple Web interface to running PHPUnit test suites? i.e. a PHP
Is there some kind of a shorthand fluent interface for creating a parameters dictionary
In interface builder, there are layout options to send to back or send to

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.