使用 serviceActivator 进行 junit 测试集成流程

问题描述 投票:0回答:1

在我的 spring-boot 项目中,我有以下课程:

@Configuration
@EnableIntegration
public class ConsumeFiles {

    @Bean
    public IntegrationFlow inboundFileFromDirectory(@Value("${period}") long period,
                                                          @Value("${poller.max.messages.per.poll}") int maxMessagesPerPoll,
                                                          MessageSource<File> fileReadingFromSrc) {
        return IntegrationFlow.from(fileReadingFromSrc,
                        c -> c.poller(Pollers.fixedDelay(period)
                        ))
                .transform(Files.toStringTransformer())
                .channel("channel-1")
                .get();
    }
    
    @ServiceActivator(inputChannel = "channel-1")
    public void controleSignature(final Message<File> file) throws IOException {
        // do things 
    }
    
        @Bean
    public MessageSource<File> fileReadingFromSrc() {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setDirectory(new File(CONST_PATH));
        source.setAutoCreateDirectory(true);
        return source;
    }
    
}

非常基本:它监视一个目录,当将文件放入其中时,流程开始(例如:控制签名文件并执行其他操作) 我想对这样的课程进行单元测试。我已经在 Spring 页面上阅读了很多资源,但我真的不擅长测试。有人可以用非常简单的方式提出建议吗?

spring-boot junit spring-integration spring-integration-dsl
1个回答
0
投票

我设法使用文档创建了一个简单的测试类:

package fr.edf.ekrs.backend.webapp.integrationFlows;

import fr.edf.ekrs.backend.webapp.ftp.ConsumesJUNIT;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.test.context.MockIntegrationContext;
import org.springframework.integration.test.context.SpringIntegrationTest;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.io.File;
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;

@ExtendWith({SpringExtension.class, MockitoExtension.class})
@ContextConfiguration(classes = ConsumesJUNIT.class)
@SpringBootTest
@SpringIntegrationTest
public class ConsumesJUNITTest {

    @Autowired
    private ConsumesJUNIT consumesJUNIT;

    @MockBean
    private MessageSource<File> fileReadingFromSrc;

    @Mock
    private Message<File> fileMessage;

    @Value("${period}")
    private long period;

    @Value("${poller.max.messages.per.poll}")
    private int maxMessagesPerPoll;

    @Autowired
    private MockIntegrationContext mockIntegrationContext;

    private File testFile;

    @BeforeEach
    public void setup() throws IOException {
        testFile = File.createTempFile("test", ".txt");
        doNothing().when(fileMessage).getPayload();
    }

    @Test
    public void testInboundFileFromDirectory() {
        IntegrationFlow flow = consumesJUNIT.inboundFileFromDirectory(period, maxMessagesPerPoll, fileReadingFromSrc);
        assertThat(flow).isNotNull();
    }

    @Test
    public void testControleSignature() throws IOException {
        consumesJUNIT.controleSignature(fileMessage);
        verify(fileMessage).getPayload();
    }

    @Test
    public void testFileProcessingFlow() {
        // Sending a file message to the input channel
        mockIntegrationContext.substituteMessageSourceFor("fileReadingFromSrc", fileReadingFromSrc);

        GenericMessage<File> message = new GenericMessage<>(testFile);
        MessageChannel inputChannel = mockIntegrationContext.getBean("fileReadingFromSrc", MessageChannel.class);
        inputChannel.send(message);

        // Verify that the file is processed correctly
        PollableChannel outputChannel = mockIntegrationContext.getBean("channel-1", PollableChannel.class);
        Message<?> receivedMessage = outputChannel.receive(0);
        assertThat(receivedMessage).isNotNull();
        assertThat(receivedMessage.getPayload()).isEqualTo(testFile);
    }
}

但由于编译错误,我仍然无法执行它(mockIntegrationContext.getBean => 未找到)

© www.soinside.com 2019 - 2024. All rights reserved.