Powerful static method PowerMockito does not work when calling a method by parameter

I am trying to test a class that uses a calculator class with a number of static methods. I successfully mocked another class in a similar way, but this one turned out to be more stubborn.

It seems that if the mocked method contains a method call on one of the arguments passed, the static method does not mock (and test breaks). Removing an internal call is clearly not an option. Is there something obvious I'm missing here?

Here's a condensed version that behaves the same ...

public class SmallCalculator {

    public static int getLength(String string){

        int length = 0;

        //length = string.length(); // Uncomment this line and the mocking no longer works... 

        return length;
    }
}

And here is the test ...

import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;

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

import com.solveit.aps.transport.model.impl.SmallCalculator;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ SmallCalculator.class})
public class SmallTester {

    @Test
    public void smallTest(){

        PowerMockito.spy(SmallCalculator.class);
        given(SmallCalculator.getLength(any(String.class))).willReturn(5);

        assertEquals(5, SmallCalculator.getLength(""));
    }
}

, , "" . , , . SmallCalculator :

public class BigCalculator {

    public int getLength(){

        int length  = SmallCalculator.getLength("random string");

        // ... other logic

        return length;
    }

    public static void main(String... args){

        new BigCalculator();
    }
}

...

import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;

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

import com.solveit.aps.transport.model.impl.BigCalculator;
import com.solveit.aps.transport.model.impl.SmallCalculator;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ SmallCalculator.class})
public class BigTester {

    @Test
    public void bigTest(){

        PowerMockito.spy(SmallCalculator.class);
        given(SmallCalculator.getLength(any(String.class))).willReturn(5);

        BigCalculator bigCalculator = new BigCalculator();
        assertEquals(5, bigCalculator.getLength());
    }
}
+4
4

https://blog.codecentric.de/en/2011/11/testing-and-mocking-of-static-methods-in-java/

, . ( ), . Simples...

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest({ SmallCalculator.class})
public class BigTester {

    @Test
    public void bigTest(){

        PowerMockito.mockStatic(SmallCalculator.class);
        PowerMockito.when(SmallCalculator.getLength(any(String.class))).thenReturn(5);

        BigCalculator bigCalculator = new BigCalculator();
        assertEquals(5, bigCalculator.getLength());
    }
}
+12

anyString() any(String.class).

any(String.class) null, Mockito , null. .

anyString() .

, , , , .

+1

:

given(SmallCalculator.getLength(any(String.class))).willReturn(5);

. , .

-, :

@PrepareForTest({ SmallCalculator.class, String.class})

, length(); :

given(String.length()).willReturn(5);

, ;)

+1

, .

when(myMethodcall()).thenReturn(myResult);

doReturn(myResult).when(myMethodCall());

, , .

, , mockStatic(SmallCalculator.class)

PowerMockito.spy(SmallCalculator.class);

0
source

Source: https://habr.com/ru/post/1599308/


All Articles