尝试存根已经被存根的模拟,我得到了UnfinishedStubbingException

时间:2019-03-17 09:49:55

标签: java junit mocking mockito powermockito

$query_args = array (
    'post_count' => -1,
    'post_type' => 'kanga_kurs',
    'fields' => 'ids',
    'meta_query' => array(
        'relation' => 'AND',
        'start_date' => array(
            'key' => '_startdatum',
            'compare' => 'EXISTS',
        ),
        'start_time' => array(
            'key' => '_startzeit',
            'compare' => 'EXISTS',
        ),
        'kursort' => array(
            'key' => '_kursort',
            'value' => $this->get_id(),
            'compare' => 'LIKE',
        ),
    ),
    'orderby' => array(
        'start_date' => 'ASC',
        'start_time' => 'ASC',
    ),
);

这是我的一种方法的测试用例,其中一个值被转换为另一种格式。因此,我正在检查ExpectedConvertedValue和ActualConvertedValue。

setHardCodeValuesToStudentObject()正在为学生对象设置一些硬编码值,例如(name =“ jack”,age =“ 12”等。)在此方法中,存在一个模拟,并且在该模拟上发生了存根。我需要模拟一个方法,因为在setHardCodeValuesToStudentObject()中,自动生成了getValue()的硬编码值。当在该行之前添加其他代码行时,它将递增。所以我不得不嘲笑那个值。

当我尝试在此(setHardCodeValuesToStudentObject())方法中模拟一个方法时,则将其抛出此----->

public void testFunction() throws Exception {

    ABC mock1= mock(ABC.class);
    when(mock1.getValue()).thenReturn("string1");
    whenNew(ABC.class).withNoArguments().thenReturn(mock1);
    Student student= setHardCodeValuesToStudentObject();

    Method method = Person.class.getDeclaredMethod("conversion", 
                                                     Student.class);
    method.setAccessible(true);
    String actualConvertedValue = (String)method.invoke(new Person(), 
                                                          student);


    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream inputStream = 
         classLoader.getResourceAsStream("expectedConvertedValue.json");
    String expectedConvertedValue= "";
    try {
        expectedConvertedValue= IOUtils.toString(inputStream, 
                                       StandardCharsets.UTF-8);
    }catch (Exception e) {
        System.out.print(e.printStackTrace());       
       }
    assertEquals(expectedConvertedValue,actualConvertedValue );
    }

0 个答案:

没有答案