Mocking dependency that has setListener(...)

Gant

My class under test has a few dependencies. All of these provide setListener() as a way to receiving notification from their non-blocking operations.

I implemented a blocking method that aggregates the results from all the non-blocking ops. Which mean I have to register the listeners using such setListener() methods, and wait for the callbacks.

How should I mock/fake these dependencies in my unit test? I could subclass them and implement setListener() and fire the callbacks as necessary. But let's say some of these deps are final class. Also, I think there might be something I could use from Mockito?

Conceptual code (untested):

public void blockingMethod() {
  CountDownLatch signal = new CountDownLatch(2);

  dep1.setListener(new Dep1Listener() {
    @Override public onResult(int result) {
      signal.countDown();
    }
  });
  dep1.calculateValue1();

  dep2.setListener(new Dep2Listener() {
    @Override public onResult(int result) {
      signal.countDown();
    }
  });
  dep2.calculateValue2();

  signal.await();
  return combinedResult;
}
Duncan Jones

I would create concrete implementations of your dependencies that return fixed values. I wouldn't subclass existing classes, instead create minimal implementations of your interfaces. If you don't have interfaces defined for the dependencies, create them.

Mocking may work, but the tests would be harder to read. As soon as a mock needs to hold onto an argument (i.e. your listener) and do something with it later, it becomes challenging.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mocking a dependency with AutoFixture

From Dev

Mocking Service dependency in angularjs tests

From Dev

Spring: Overriding/Mocking dependency's dependency

From Dev

Angular Testing - Trouble mocking Service with $http dependency

From Dev

node js unit testing: mocking require dependency

From Dev

SetListener for View in Xamarin

From Dev

setlistener: errors with jack and alsa

From Java

How to do jest mocking for non-exist external dependency

From Dev

How to do basic dependency injection in Python (for mocking/testing purposes)

From Dev

TeamCity already has a dependency defined for

From Dev

A package dependency has the wrong name

From Dev

Mocking a method with different signatures where one has Object as a parameter type

From Dev

Mocking MongoCollection crashes - Exception has been thrown by the target of an invocation

From Dev

Junit testing with two,three dependency classes mocking, stubs. any recommendation for Junit tutorial and examples

From Java

NuGet: 'X' already has a dependency defined for 'Y'

From Java

Is the "useEffect has a missing dependency" warning sometimes wrong?

From Java

How to check if gradle dependency has new version

From Dev

Why dependency properties in WPF has to be Static

From Java

React Hook useMemo has a missing dependency: 'handleClearData'

From Java

React Hook useEffect has a missing dependency: 'dispatch'

From Dev

Imported dependency injection configuration has no container

From Dev

React | React Hook useEffect has a missing dependency

From Dev

React Hook useEffect has a missing dependency: 'getNewPins'

From Dev

Service is not ignored even if it has missing dependency

From Dev

SimpleInjector: Registration of class that has data parameters, and a dependency

From Dev

android animate() withEndAction() vs setListener() onAnimationEnd()

From Java

React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use

From Dev

Maven transitive dependency has scope compile while when dependency has provided scope

From Java

My App's "React Hook useEffect has a missing dependency" warning

Related Related

  1. 1

    Mocking a dependency with AutoFixture

  2. 2

    Mocking Service dependency in angularjs tests

  3. 3

    Spring: Overriding/Mocking dependency's dependency

  4. 4

    Angular Testing - Trouble mocking Service with $http dependency

  5. 5

    node js unit testing: mocking require dependency

  6. 6

    SetListener for View in Xamarin

  7. 7

    setlistener: errors with jack and alsa

  8. 8

    How to do jest mocking for non-exist external dependency

  9. 9

    How to do basic dependency injection in Python (for mocking/testing purposes)

  10. 10

    TeamCity already has a dependency defined for

  11. 11

    A package dependency has the wrong name

  12. 12

    Mocking a method with different signatures where one has Object as a parameter type

  13. 13

    Mocking MongoCollection crashes - Exception has been thrown by the target of an invocation

  14. 14

    Junit testing with two,three dependency classes mocking, stubs. any recommendation for Junit tutorial and examples

  15. 15

    NuGet: 'X' already has a dependency defined for 'Y'

  16. 16

    Is the "useEffect has a missing dependency" warning sometimes wrong?

  17. 17

    How to check if gradle dependency has new version

  18. 18

    Why dependency properties in WPF has to be Static

  19. 19

    React Hook useMemo has a missing dependency: 'handleClearData'

  20. 20

    React Hook useEffect has a missing dependency: 'dispatch'

  21. 21

    Imported dependency injection configuration has no container

  22. 22

    React | React Hook useEffect has a missing dependency

  23. 23

    React Hook useEffect has a missing dependency: 'getNewPins'

  24. 24

    Service is not ignored even if it has missing dependency

  25. 25

    SimpleInjector: Registration of class that has data parameters, and a dependency

  26. 26

    android animate() withEndAction() vs setListener() onAnimationEnd()

  27. 27

    React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use

  28. 28

    Maven transitive dependency has scope compile while when dependency has provided scope

  29. 29

    My App's "React Hook useEffect has a missing dependency" warning

HotTag

Archive