繁体   English   中英

在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);
}

上下文类如下:

@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();
}

我只想让它返回true。 但是我不知道该在哪里“存根”或如何存根。

为什么不将那个fileMover做到@Bean

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

暂无
暂无

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

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