lundi 5 avril 2021

No code coverage when using PowerMock to test final class with static methods Java

I am writing a test case for a final class that has static methods defined inside it. Now the test case works fine , but when I click on code coverage it says 0% , although the test case runs fine.

I am using PowerMock along with EasyMock and using Junit4.

My class

public class Meals {
    public static String getName(String name) {
        if (name == null) {
            return "bad";
        } else {
            return "good";
        }
    }
}

And My test case

import static org.junit.Assert.assertEquals;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Meals.class)
public class MealsTest {

    @Before
    public void setUp() throws Exception {
        PowerMock.mockStatic(Meals.class);
    }

    @Test
    public void testMeals() {

        EasyMock.expect(Meals.getName(null)).andReturn("bad");
        PowerMock.replayAll();
        assertEquals("bad", Meals.getName(null));
        PowerMock.verifyAll();

    }

}

According to me it is because of the annotations Preparefortest and RunWith , but i'm not sure.

So any help would be appreciated .

Aucun commentaire:

Enregistrer un commentaire