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

How do you unit test a JavaFX controller with JUnit

In Angularjs how do you alter a mock service per test?

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

how do you mock an xml for unit testing?

How do you test StreamBuilder in Flutter using Mockito?

How to mock the static method with mockito to do unit test

How do you unit test a Celery task?

How do you unit test a Celery task?

How do you skip a unit test in Django?

How do you use test-unit?

How do you Unit Test Python DataFrames

How do you unit test private methods?

How do you unit test Django RawQuerySets

How do you moq a class for unit test

How do I unit test (with JUnit or mockito) recyclerview item clicks

how to do a Unit Test with Junit and mockito in java on methods that prefer dates

In maven, how do you run separate configurations for different junit test classes

How do you Mock LUIS during Unit Tests?

How to test and mock a GRPC service written in Java using mockito

How do you test django-axes with a Django unit test?

How to unit test setter method in Mockito JUnit

How do you Test a service where Maui.Storage.ISecureStorage is used

How do you mock useLocation() pathname using shallow test enzyme Reactjs?

How do you test, mock context in React tests?

How do you mock a function that is part of that class under test gmock?

How do you mock/test requestjs requestcallback with Jest

Spring Mockito - Junit Controller Test - Mock one service

How do you unit test dataweave code using Mulesoft's Dataweave Assersion Library?

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