在 spring 集成上下文中存根模拟 bean 中的方法

stub a method in mock bean in spring integration context

我正在尝试测试 spring 集成设置 我的单元测试如下,

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { SIContext2Config.class })
public class FileMoverTest extends GenericTest {

@Test
public void testFileMover() throws InterruptedException {
    Thread.sleep(1000);
    int total = 2;
    int n = 0;
    for (int i = 1; i <= total; i++) {
        @SuppressWarnings("unchecked")
        Message<OfferMessage> msg = (Message<OfferMessage>) hdfsReadyChannel.receive(2000);
        System.out.println("Message # " + i + " received:" + msg.getPayload().getOfferFile().getAbsolutePath());
        n++;
    }
    Assert.state(n == total);
}

上下文class如下:

@Configuration
public class SIContext2Config { 

@Mock
private FsShell fsh;

@InjectMocks
@Mock
private MoveToHdfs fileMover;

@Bean
public Ingester ingester() {
    return new Ingester(fileMover);
}

@Mock
private Ingester ingester;

@Bean
public FilePickupHandler filePickupHandler() {
    return new FilePickupHandler();
}
}

现在,这是我正在尝试做的事情: Ingester bean 有一个名为 handle() 的方法,MoveToHdfs 对象 fileMover 在该方法中运行并调用 move()。

public OfferMessage handle(Message<OfferMessage> msg) {
    // get hive directory path
    String remotePath = msg.getPayload().getOfferComponent().getHiveDirectory();
    String localFile = msg.getPayload().getOfferFile().getAbsolutePath();
    LOGGER.debug("Moving file {} to remote path:{}", localFile, remotePath);

    if (!fileMover.move(localFile, remotePath, true)) {
            throw new SomeException();
    }

    return msg.getPayload();
}

我只想 return 是真的。 但我无法弄清楚 "stub" 在哪里,或者如何存根。

为什么不将 fileMover 修改为 @Bean

@Bean
public MoveToHdfs fileMover() {
    MoveToHdfs fileMover = Mockito.mock(MoveToHdfs.class);
    when(fileMover.move(anyString(), anyString(), anyBoolean())).thenReturn(true);
    return fileMover;
}