How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

BrianP

I want to write a unit test for a service that uses/depends on another class. What i'd like to do is mock the behavior of the dependent class (As opposed to an instance of that class). The service method being tested uses the dependent class internally (i.e. an instance of the dependent class isn't passed in to the method call) So for example I have a service method that I want to test:

import DependentClass;

public class Service {

    public void method() {
        DependentClass object = new DependentClass();
        object.someMethod();
    }
}

And in my unit test of Service method(), I want to mock someMethod() on the DependentClass instance instead of having it use the real one. How do I go about setting that up in the unit test?

All of the examples and tutorials i've seen show mocking object instances that are passed in to the method being tested, but I haven't seen anything showing how to mock a class as opposed to an object instance.

Is that possible with Mockito (Surely it is)?

troig

It's easy with Powermockito framework and whenNew(...) method. Example for your test as follows:

   @Test
   public void testMethod() throws Exception {
      DependentClass dependentClass = PowerMockito.mock(DependentClass.class);
      PowerMockito.whenNew(DependentClass.class).withNoArguments().thenReturn(dependentClass);

      Service service = new Service();
      service.method();
   }

Hope it helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you test a service in spring mvc with mockito, rxjava2 using an io scheduler?

From Dev

how do you mock an xml for unit testing?

From Dev

how do you mock an xml for unit testing?

From Dev

How do you test StreamBuilder in Flutter using Mockito?

From Java

How do you unit test private methods?

From Java

How do you skip a unit test in Django?

From Dev

How do you use test-unit?

From Dev

How do you inject an angular2 service into a unit test? (RC3)

From Dev

Do unit test mocking frameworks force you in the way you're programming?

From Dev

Spring Mockito - Junit Controller Test - Mock one service

From Dev

How do you unit test that methods such as ok() were called in aurelia-dialog using jasmine?

From Dev

How do you unit test that methods such as ok() were called in aurelia-dialog using jasmine?

From Dev

Unit Test, Should you subclass the class you're testing?

From Dev

TDD In practice -- How do you write a unit test for sending an email?

From Dev

How do you write a unit test for a function that is not exported?

From Dev

How do you test for values created in a new Buffer in unit tests

From Dev

How do you override a module/dependency in a unit test with Dagger 2.0?

From Dev

How do you unit test Google Cloud NDB code?

From Dev

how to mock $window to unit test AngularJS service?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How can you select specific Google Mock test cases/unit tests to run?

From Dev

How can you select specific Google Mock test cases/unit tests to run?

From Dev

Unit Test MapReduce - Junit Mockito

From Dev

In a unit test, do you verify and assert?

From Dev

In a unit test, do you verify and assert?

From Dev

How do you test service or controller methods in grails 2.3

From Dev

How do you test Angular 2 directive that calls a service?

From Dev

How do you test service or controller methods in grails 2.3

Related Related

  1. 1

    How do you test a service in spring mvc with mockito, rxjava2 using an io scheduler?

  2. 2

    how do you mock an xml for unit testing?

  3. 3

    how do you mock an xml for unit testing?

  4. 4

    How do you test StreamBuilder in Flutter using Mockito?

  5. 5

    How do you unit test private methods?

  6. 6

    How do you skip a unit test in Django?

  7. 7

    How do you use test-unit?

  8. 8

    How do you inject an angular2 service into a unit test? (RC3)

  9. 9

    Do unit test mocking frameworks force you in the way you're programming?

  10. 10

    Spring Mockito - Junit Controller Test - Mock one service

  11. 11

    How do you unit test that methods such as ok() were called in aurelia-dialog using jasmine?

  12. 12

    How do you unit test that methods such as ok() were called in aurelia-dialog using jasmine?

  13. 13

    Unit Test, Should you subclass the class you're testing?

  14. 14

    TDD In practice -- How do you write a unit test for sending an email?

  15. 15

    How do you write a unit test for a function that is not exported?

  16. 16

    How do you test for values created in a new Buffer in unit tests

  17. 17

    How do you override a module/dependency in a unit test with Dagger 2.0?

  18. 18

    How do you unit test Google Cloud NDB code?

  19. 19

    how to mock $window to unit test AngularJS service?

  20. 20

    How to mock AngularFire 2 service in unit test?

  21. 21

    How to mock AngularFire 2 service in unit test?

  22. 22

    How can you select specific Google Mock test cases/unit tests to run?

  23. 23

    How can you select specific Google Mock test cases/unit tests to run?

  24. 24

    Unit Test MapReduce - Junit Mockito

  25. 25

    In a unit test, do you verify and assert?

  26. 26

    In a unit test, do you verify and assert?

  27. 27

    How do you test service or controller methods in grails 2.3

  28. 28

    How do you test Angular 2 directive that calls a service?

  29. 29

    How do you test service or controller methods in grails 2.3

HotTag

Archive