Mocking static functions from other classes in Java using PowerMockito

kaushalpranav

I have written test cases for testing a function in class Main, called functionMain(). I have seen people using PowerMockito to test static functions in the class Main which is under test.

But in my case, functionMain() is using a static function from another class called Branch called staticBranchFunction().

I want to mock staticBranchFunction() inside the tests for the Main class.

This main function actually has calls to static functions from different classes Branch1, Branch2 etc.

Please help.

uli
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Boom.class})
public class DocTest {

    public String boomWrapper() {
        return Boom.detonate();
    }

    @Test
    public void testBoom() {
        mockStatic(Boom.class);
        when(Boom.detonate()).thenReturn("defused");
        String actual = boomWrapper();
        verifyStatic(Boom.class, times(1));
        Boom.detonate();
        assertEquals("defused", actual);
    }    
}

class Boom {
    private static final String BOOM = "Boom!";  
    public static String detonate() {
        return BOOM;
    }
}

Dependencies:

junit:junit:4.12  
org.mockito:mockito-core:2.13.0  
org.powermock:powermock-module-junit4:2.0.0-beta.5  
org.powermock:powermock-api-mockito2:2.0.0-beta.5  

Description:

For more supported versions, please read: Mockito + PowerMock, other supported frameworks Requirements:

  • List all static classes in @PrepareForTest({Boom.class}) separate by comma.
  • Mock all static classes by PowerMockito.mockStatic(Boom.class) separate by comma.
  • use regular mockito methods to setup your expectation, for example Mockito.when(Boom.detonate()).thenReturn("defused")
  • verify behavior by PowerMockito.verifyStatic(Boom.class, Mockito.times(1)); Boom.detonate(); Important: You need to call PowerMockito.verifyStatic(Boom.class) per method verification.

More details on PowerMock wiki.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mocking a static method using powermockito

Mocking static class with powermockito

PowerMockito throwing exception when mocking static method of an enum and using that enum object inside a switch statement

Mocking a private method in junit using Powermockito

Java PowerMockito Mocking Instant.now()

PowerMockito mocking static method fails when calling method on parameter

PowerMockito: got InvalidUseOfMatchersException when use matchers mocking static method

Mocking Static Blocks in Java

How to mock several static classes by PowerMockito

Importing Functions from Other Classes in Scala

Using classes from other modules

Java no response from other classes

Mocking local object creation of a final class using Powermockito

Exception while using PowerMockito with static method

Verify Static Method Call using PowerMockito 1.6

MissingMethodInvocationException when using PowerMockito to test static method

Static functions outside classes

Classes with static arrow functions

Mocking Functions Using Python Mock

Objects made in static calls in java/static mocking

Disadvantages of using all static methods as opposed to other "service" classes?

Initialization of map from static data member of other classes

static functions using generic types in Java

Using a homemade library of classes as functions in other class files

Dynamically adding static functions to class from other class

Connecting to localhost from Docker (Java Application using localstack for mocking AWS)

updating labels from other classes in java fx

Calling variables from other classes in Java

How to process elements from other classes in java

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive