简体   繁体   English

spring 云 stream function 基于方法的单元测试怎么写?

[英]How to write unit test for spring cloud stream function based method?

When I try to test a spring cloud stream function based method, it always happens NullPointerException about InputDestination.当我尝试测试基于 spring cloud stream function 的方法时,它总是发生关于 InputDestination 的 NullPointerException。

I have two questions:我有两个问题:

  1. It's hard for me to know how to write UT from the official doc.我很难从官方文档中知道如何编写 UT。 official test doc 官方测试文档
  2. Besides, how to write integration Test if test file has some dependencies.此外,如果测试文件具有某些依赖项,如何编写集成测试。 It seems create a new context and always has NoSuchBeanDefination error.它似乎创建了一个新的上下文并且总是有 NoSuchBeanDefination 错误。

I have tried as flow, but the context can not find some dependency beans.我试过 as flow,但是上下文找不到一些依赖 bean。

@Test
public void sampleTest() {
    try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
                TestChannelBinderConfiguration.getCompleteConfiguration(
                        MyTestConfiguration.class))
                .run("--spring.cloud.function.definition=uppercase")) {
        InputDestination source = context.getBean(InputDestination.class);
        OutputDestination target = context.getBean(OutputDestination.class);
        source.send(new GenericMessage<byte[]>("hello".getBytes()));
        assertThat(target.receive().getPayload()).isEqualTo("HELLO".getBytes());
    }
}

So I just want to write UT, but still have NPE.所以我只想写UT,但是还有NPE。

Here is my code.这是我的代码。


    @Bean
    public Function<Message<List<DemoBean>>, Message<DemoBean>> findFirstBean( ){
        return message -> {
            List<DemoBean> demoBeans =  message.getPayload();
            return MessageBuilder.withPayload(demoBeans.get( 0 )).build();
        };
    }

Here is my test.这是我的测试。


    @SpringBootTest
    @ActiveProfiles(profiles = "local")
    @Import({ TestChannelBinderConfiguration.class})
    class FunctionDemoTest {

    @Autowired
    private InputDestination inputDestination;
    @Autowired
    private OutputDestination outputDestination;
    private FunctionDemo functionDemo;
    // some dependency need to mock
    private DemoService demoService;


    @BeforeEach
    void setUp() {
        demoService = Mockito.mock( DemoService.class );
        functionDemo = new FunctionDemo( demoService);
    }


    @Test
    public void findFirstBeanTest() {
        DemoBean demoBean = new DemoBean();
        demoBean.setName("Howard");
        demoBean.setAge( 1 );
        DemoBean demoBean1 = new DemoBean();
        demoBean1.setName("Frank");
        demoBean1.setAge( 2 );
        List<DemoBean> demoBeanList = new ArrayList<>();
        demoBeanList.add( demoBean );
        demoBeanList.add( demoBean1 );

        Message<List<DemoBean>> inputMessage = MessageBuilder.withPayload(demoBeanList).build();
        inputDestination.send(inputMessage,"findFirstBean-in-0");
        
        Assertions.assertNotNull(  outputDestination.receive( 10000, "findFirstBean-out-0") );

    }


    }

Here is error:这是错误:

java.lang.NullPointerException: while trying to invoke the method org.springframework.messaging.SubscribableChannel.send(org.springframework.messaging.Message) of a null object returned from org.springframework.cloud.stream.binder.test.InputDestination.getChannelByName(java.lang.String)

    at org.springframework.cloud.stream.binder.test.InputDestination.send(InputDestination.java:89)
    at com.successfactors.caf.listener.FunctionDemoTest.raePdrResultProcessor(FunctionDemoTest.java:82)

Well, I know the root cause of NPE.嗯,我知道 NPE 的根本原因。

Message<byte[]> receive(long timeout, String bindingName)

It seems should be destinationName instead of bindingName in source code.在源代码中似乎应该是 destinationName 而不是 bindingName 。

Any other answers would be appreciated.任何其他答案将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM