2016-01-13 52 views
0

我有一个叫SampleClass的类。这有很多静态字段,这个类目前在其他一些类中使用。我是Mockito和Power Mockito的新手。我一直在努力嘲笑SampleClass。你们可以请帮助让我知道如何嘲讽SampleClass使用Mockito和PowerMockito创建模拟对象

我已经在下面提供了SampleClass及其使用细节供您参考。

SampleClass.java:在其它的类SampleClass的

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.getInstance(); 
String code = "ABC"; 
Cat cat = sampleClass.getCat(code); 
CATUtil catUtil = new CATUtil(cow); 
+0

到底是什么问题?你在嘲笑上述课程时是否有错误? –

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