15
@Mocked Provider provider;

public static class Provider {
    public static List<Integer> getStaticList() {
        return new ArrayList<>();
    }

    public static List<Integer> test() {
        return getStaticList();
    }
}

@Test
public void testStatic() {
    ArrayList<Object> list = new ArrayList<>();
    list.add(1);

    new Expectations() {
        {
            Provider.getStaticList();
            result = list;
        }
    };

    assertThat(Provider.test(), JUnitMatchers.hasItem(1));
}

I want to mock (with JMockit) one static method that is called within another one. How can I do that? The above test fails. The real Provider.test method is never called.

T3rm1
  • 2,299
  • 5
  • 34
  • 51

3 Answers3

11

The code below changes the behaviour of doSomething static method only, without affecting other static methods.

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.util.HashMap;
import java.util.Map;

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({Foo.class})
public class Snipets {

    @Test
    public void hoge() throws Exception {
        PowerMockito.spy(Foo.class);
        PowerMockito.when(Foo.class, "doSomething").thenReturn("dummy");

        String actual = Foo.doSomething();
        assertEquals("dummy", actual);
    }

}

Foo.java

public class Foo {
    public static String doSomething() {
        return "foo";
    }
}

Source: https://gist.github.com/mid0111/8859159

cahen
  • 15,807
  • 13
  • 47
  • 78
8

I used a very simple method of writing a conditional answer like this:

    PowerMockito.mockStatic(<MyMockedClass>.class, invocation -> {
        if (invocation.getMethod().getName().equals("<methodToMockName>")) {
            return <mockedValue>;
        }
        return invocation.callRealMethod();
    });
Marcin T.P. Łuczyński
  • 3,255
  • 1
  • 20
  • 15
7

You can use partial mocking:

@Test
public void testStatic() {
    new Expectations(Provider.class) {{ Provider.getStaticList(); result = 1; }};

    List<Integer> test = Provider.test();

    assertTrue(test.contains(1));
}

(With no "@Mocked Provider" field for the test above.)

Rogério
  • 16,171
  • 2
  • 50
  • 63