使用Mockito和PowerMockito创建Mock对象

时间:2016-01-13 13:53:22

标签: mockito powermock powermockito

我正在上一个名为SampleClass的班级。这有许多静态字段,这个类目前在其他一些类中使用。我是Mockito和Power Mockito的新手。我一直在努力模仿SampleClass。请你告诉我如何模仿SampleClass

,请你们帮助

我在下面提供了SampleClass及其用法详情供您参考。

SampleClass.java:

public class SampleClass {
    private static CatFactory catFactory = null;
    private static String catPath = SampleConfigurationManager.getInstance().getProperty("locale_path", "");
    private static URI catURI = (new File(catPath)).toURI();

    private static SampleClass catBusinessLogic;
    private static Logger logger = Logger.getInstance(SampleClass.class);


    private SampleClass() {
        initializeCatFactory();
    }

    public static SampleClass getInstance() {   
        try{
            if(catBusinessLogic == null) {
                catBusinessLogic = new SampleClass();   
            }
        }catch(Exception e ){
            e.printStackTrace();
        }

        return catBusinessLogic;
    }

    public Cat getCat(String code) throws CatException {
        logger.log(LogLevel.DEBUG, "SampleClass::getCat ENTRY");
        Cat countryCat = null;
        if (catFactory != null) {
            countryCat = catFactory.getCat(code);
        }
        logger.log(LogLevel.DEBUG, "SampleClass::getCat EXIT");

        if( countryCat == null ) {
            throw new CatException("Failed to create CAT object");
        }
        return countryCat;
    }

    public static void initializeCatFactory() throws CatException{
        logger.log(LogLevel.DEBUG, "SampleClass::initializeCatFactory ENTRY");
        try {
            catFactory = new CatFactory(catURI.toURL());
        } catch (MalformedURLException mue) {
            logger.log(LogLevel.FATAL, "MalformedURLException while creating CATFactory " + mue.toString());
            throw new CatException("Failed to create CATFactory");
        }
        logger.log(LogLevel.DEBUG, "SampleClass::initializeCatFactory EXIT");
    }
}

在其他一些类中使用SampleClass:

SampleClass sampleClass = SampleClass.getInstance();
String code = "ABC";
Cat cat = sampleClass.getCat(code);
CATUtil catUtil = new CATUtil(cow);

1 个答案:

答案 0 :(得分:0)

如果您有单身SampleClass。贝娄样本单例类代码。

public class SampleClass {
    private static SampleClass INSTANCE;

    public static SampleClass getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SampleClass();
        }

        return INSTANCE;
    }

    public String someMethod() {
        return "hello";
    }
}

您使用SampleClass的下一个课程就像这样。

public class SomeOtherClass {
    public String hello(){
        return SampleClass.getInstance().someMethod() + "+other";
    }
}

如果你想模拟你的单身SampleClass类,你的代码可能如下所示:

@RunWith(PowerMockRunner.class)
@PrepareForTest(SampleClass.class)
public class SompleTest {

    @Test
    public void someTest() throws Exception {
        //GIVEN
        //create a mock (you can use Mockito or PowerMock)
        SampleClass mock = Mockito.mock(SampleClass.class);
        Mockito.when(mock.someMethod()).thenReturn("test");

        //when object `SampleClass` is created, then return mock
        PowerMockito.whenNew(SampleClass.class).withNoArguments().thenReturn(mock);

        //WHEN
        String hello = new SomeOtherClass().hello();

        //THEN
        Assert.assertEquals("test+other", hello);
    }
}