2014-02-06 74 views
1

我有恩Platform.getBundle(Activator.PLUGIN_ID)返回null

public void func { 
    .... 
    .... 
    URL url = Platform.getBundle(Activator.PLUGIN_ID).getEntry("config"); 
    .... 
    .... 
} 

现在我写的JUnit测试案例上述功能,该功能在不同的测试插件的功能。 当我在我的junit测试方法中调用func时,我收到空指针异常。

我试着打印

Platform.getBundle(Activator.PLUGIN_ID) which returns null 
Platform.isRunning() which returns false. 

请让我知道如何解决这个问题?我可以做什么,以便我的JUnit测试用例成功。

预先感谢您。

+1

可以移动Platform.getBundle(Activator.PLUGIN_ID)的方法和存根方法为您的测试 –

+0

你能否更详细的吧。我对存根和嘲笑很陌生。我不知道如何存根。逻辑代码会有所帮助。 – user2179627

+0

基本上将您的“Platform.getBundle(Activator.PLUGIN_ID)”移动到方法并在测试期间使用ovverride方法 –

回答

0
Just a Pseudo Code... 

     private Platform platform; 

     public void func() { 
       //.... 
       //.... 
       URL url = getPlatForm().getEntry("config"); 
       //.... 
       //.... 
      } 

     public Platform getPlatForm() { 
      return platform.getBundle(Activator.PLUGIN_ID); 
     } 


In your testcase, 

    override the getPlatForm() method with a mock/stub. 


    Class yourClass = new yourClass() { 
    @override 
    public Platform getPlatForm() { 
     return mock/stub!!; 
    } 
    } 
+0

非常感谢。我知道了。我重写了该方法,并返回了所需的URL。 – user2179627

+0

如果我的回答对你有帮助,请投票:) –

+0

我还有一个疑问。如果调用URL url = Platform.getBundle(Activator.PLUGIN_ID).getEntry(“config”);在构造函数中有吗?我们如何解决它。把它放在一个方法中,并在构造函数中调用它并不是我相信的一个好主意。 – user2179627