org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized field "channel" (class xyz.coins.biz.service.domain.fiat.SwiftpayOtcDepositRequestBO), not marked as ignorabl e; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized fi eld "channel" (class xyz.coins.biz.service.domain.fiat.SwiftpayOtcDepositRequestBO), not marked as ignor able (8 known properties: "currency", "cashAgent", "customerEmailAddress", "amount", "orderId", "source" , "description", "channelName"]) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 47] (through reference chain: xyz.coins.biz.service.domain.fiat.SwiftpayOtcDepositRequestBO["channel"]) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(Abs tractJackson2HttpMessageConverter.java:391)

2023-06-16by@lvxing_ood-hcvmu&@单元测试.Model: gpt-3.5
lvxing_ood-hcvmu

单元测试Mock用哪个好

Avatar
lvxing_ood-hcvmu

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized field
"channel" (class xyz.coins.biz.service.domain.fiat.SwiftpayOtcDepositRequestBO), not marked as ignorabl
e; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized fi
eld "channel" (class xyz.coins.biz.service.domain.fiat.SwiftpayOtcDepositRequestBO), not marked as ignor
able (8 known properties: "currency", "cashAgent", "customerEmailAddress", "amount", "orderId", "source"
, "description", "channelName"])
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 47] (through
reference chain: xyz.coins.biz.service.domain.fiat.SwiftpayOtcDepositRequestBO["channel"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(Abs
tractJackson2HttpMessageConverter.java:391)

Avatar
Avatar

选择一个好的Mock框架取决于你的需求和偏好。以下是一些流行的Java Mock框架:

  1. Mockito:Mockito是一个简单易用的Mock框架,它提供了丰富的API来创建和验证Mock对象。它可以与JUnit和TestNG等测试框架无缝集成。

  2. EasyMock:EasyMock是另一个流行的Mock框架,它提供了类似的API和功能。它也可以与JUnit和TestNG等测试框架集成。

  3. PowerMock:PowerMock是一个功能强大的Mock框架,它可以与Mockito或EasyMock一起使用。它提供了一些额外的功能,例如模拟静态方法和私有方法。

  4. JMockit:JMockit是一个功能强大的Mock框架,它提供了广泛的功能和API。它可以与JUnit或TestNG等测试框架集成。

这些框架都有自己的优点和适用场景。你可以根据自己的需求和偏好来选择一个适合你的Mock框架。

lvxing_ood-hcvmu

mo c k be an

Avatar
Avatar

根据您的输入,您可能想询问有关MockBean的信息。MockBean是Spring Framework中的一个注解,用于在测试类中创建模拟的Bean对象。

使用MockBean注解,您可以将一个模拟对象注入到Spring应用程序的上下文中,以便在测试中使用。这对于在测试中模拟外部依赖项或不可用的组件非常有用。

以下是使用MockBean注解的示例:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceTest {

    @Autowired
    private MyService myService;

    @MockBean
    private ExternalService externalService;

    @Test
    public void testMyService() {
        // 设置模拟对象的行为
        Mockito.when(externalService.getData()).thenReturn("Mocked Data");

        // 调用被测试的方法
        String result = myService.doSomething();

        // 验证结果
        Assert.assertEquals("Mocked Data", result);
    }
}

在上面的示例中,我们使用MockBean注解来创建一个模拟的ExternalService对象,并注入到MyService中。然后,我们使用Mockito来设置模拟对象的行为,并在测试中调用被测试的方法进行验证。

需要注意的是,这个例子使用了Spring Boot和JUnit框架,但是MockBean注解也可以在其他Spring应用程序中使用。

总结来说,MockBean是Spring Framework中用于创建模拟对象的注解之一,它可以帮助您在Spring应用程序的测试中模拟外部依赖项。