How to mock only one static method and test the other

T3rm1 picture T3rm1 · Dec 8, 2015 · Viewed 8.6k times · Source
@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.

Answer

cahen picture cahen · Dec 8, 2015

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