2017-03-16 58 views
1

我想模拟调用getJSONFile(),从json文件返回someJson对象。Mocked方法,但它调用实际的

public class NameClass{ 
    public String getValues() throws Exception 
      { 
       HelpingClass e=new HelpingClass(); 
       String name=" "; 
       String age=" "; 
       String gender=" "; 
       JSONObject json= e.getJSONFile(); 
       json.getString("name"); 
       json.getString("age"); 
       json.getString("gender"); 
       System.out.println("Request received is "+json); 
       ------ I have some other logic with JSON received and return string... 

    } 
} 

这是我getJSONFile()

public class HelpingClass{ 
    public JSONObject getJSONFile() throws Exception 
     { 
      System.out.println("Getting JSON file"); 
      File f = new File("src/main/resources/input.json"); 
      JSONObject json ; 
      InputStream is = new FileInputStream(f); 
      String jsonTxt = IOUtils.toString(is); 
      json=new JSONObject(jsonTxt); 
      return json; 
     } 
} 

这是我的测试案例

@RunWith(MockitoJUnitRunner.class) 
public class TestValues { 

    @InjectMocks NameClass names; 
    @Mock HelpingClass help; 
    @Test 
    public void testgetValues() throws Exception 
    { 
     //I am expecting this output 
     JSONObject addValues=new JSONObject(); 
     addValues.put("name", "Rahul"); 
     addValues.put("age", "30"); 
     addValues.put("gender","male"); 
     //this is what i am returning when help.getJSONFile is called 
     JSONObject json=new JSONObject(); 
     json.put("name", "Rahul"); 
     json.put("age", "30"); 
     json.put("gender", "male"); 
     //Mocking this call and returning my json 
     Mockito.when(help.getJSONFile()).thenReturn(json); 
     String s=names.getValues(); 
     Assert.assertEquals(addValues.toString(),s); 
    } 
} 

现在,而不是返回所创建的JSON它返回我,我的JSON喜欢的名字在文件中Rohit年龄是28岁,性别是男性。

为什么这个嘲笑不起作用?从技术上讲,它应该调用模拟方法并返回JSONObject类的json对象(即Rahul,30岁和男性)中的值。

我哪里错了?

+1

您好像还在该方法中使用HElpingClass ..你使用Ex e = new Ex(); ..来得到json –

+0

names.getValues()返回的内容? – mhasan

+0

不,我很抱歉,那是错字。它实际上是在帮助上课。我已编辑代码请现在检查 – Rindha

回答

3

那么@InjectMocks不会工作,如果你正在实例化方法内的HelperClass。

你将不得不作出一个实例变量或使用吸气剂,你会再嘲笑:

public class NameClass{ 

    public String getValues() throws Exception 
    { 
      HelpingClass e= getHelpingClass(); 
      .... 
    } 

    HelpingClass getHelpingClass(){ 
     return new HelpingClass(); 
    } 
} 

并在测试使用@Spy类似以下内容:

public class TestValues { 

    @InjectMocks 
    @Spy 
    NameClass names; 
    @Mock HelpingClass help; 
    @Test 
    public void testgetValues() throws Exception 
    { 
     doReturn(help).when(names).getHelpingClass(); 
     Mockito.when(help.getJSONFile()).thenReturn(json); 
     ... 
    } 
    } 
+0

就是这样。 Mockito没有机会以这种方式注入模拟。请参阅http://static.javadoc.io/org.mockito/mockito-core/2.7.17/org/mockito/InjectMocks.html以了解有关'@InjectMocks'工作原理的文档。特别是“*如果以下任何一种策略失败,那么Mockito不会报告失败;也就是说您必须自己提供依赖关系。”#: – ohlec

+0

Thank you,it is getting me the values from json input I asked it it回来。但是现在我有一个File file = new File(s)。其中s是要在其中搜索内容的文件,但这没有发生。它使我赢得这条线。我有什么方法来模拟新的文件,以便它可以创建我必须搜索的新的Excel文件 – Rindha

相关问题