无法使用模拟API对此类进行单元测试

时间:2016-03-04 19:51:52

标签: powermock

它是遗留代码,我无法使用Powermock进行单元测试。如果有的话,请使用Powermock或任何其他方式帮助我在课堂下进行单元测试:

ServiceAPI类必须经过单元测试

public class ServiceAPI {

    public String getServiceKey(String serviceNumber){
        String jndiUrl = getServURL();
        return jndiUrl;
    }

    public String getServURL(){
        String url = ServiceLocator.getInstance().init();
        return url;
    }

}

上面的类依赖于必须被模拟的下面的类

public class ServiceLocator {

    private static ServiceLocator serviceLocator = new ServiceLocator();

    public static ServiceLocator getInstance(){
        return serviceLocator;
    }

    public String init(){
        return "Jndi Call has return fix name";
    }   

}

我在下面写了测试

@RunWith(PowerMockRunner.class)
@PrepareForTest(ServiceLocator.class)
public class ServiceAPITest {

    @Test
    public void testGetServiceKey() {

        ServiceLocator serviceLocator = PowerMockito.mock(ServiceLocator.class);
        PowerMockito.mockStatic(ServiceLocator.class);
        PowerMockito.when(ServiceLocator.getInstance()).thenReturn(serviceLocator);
        PowerMockito.when(serviceLocator.init()).thenReturn("value");
        ServiceAPI serviceAPI = new ServiceAPI();
        serviceAPI.getServiceKey("20156");
    }

}

没有得到o / p。请帮忙!

0 个答案:

没有答案