2016-01-13 50 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); 
    } 
}