在 Spring 集成上下文中的模拟 Bean 中存根方法



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

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

我只是希望它返回真实。但是我不知道在哪里"存根"它,或者如何存根它。

为什么不对@Bean fileMover返工呢?

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

相关内容

  • 没有找到相关文章

最新更新