T3rm1
T3rm1

Reputation: 2582

How to mock only one static method and test the other

@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.

Upvotes: 15

Views: 13946

Answers (3)

cahen
cahen

Reputation: 16706

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

Upvotes: 11

Marcin T.P. Łuczyński
Marcin T.P. Łuczyński

Reputation: 3715

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();
    });

Upvotes: 8

Rog&#233;rio
Rog&#233;rio

Reputation: 16390

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.)

Upvotes: 7

Related Questions