jeudi 12 mars 2015

stub a method in mock bean in spring context



I am trying to test a spring integration set up my unit test is as follows,



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


The context class is as follows:



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


Now, here is what I am trying to do: The Ingester bean has a method called handle(), inside which the MoveToHdfs object fileMover runs and calls 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();
}


I just want that to return true. But I can't figure out where to "stub" that, or how to stub that.




Aucun commentaire:

Enregistrer un commentaire