I am trying to create a new file using RxJava on Android, like this:
public Observable<Boolean> createRx(String name) {
return Observable.just(name)
.map(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
File newFile = new File(localPath + "/" + s);
try {
return newFile.createNewFile();
} catch (IOException e) {
throw Exceptions.propagate(e);
}
}
});
}
To create a new file normally, like this:
public boolean createNonRx(String name) {
boolean ret = false;
try {
File newFile = new File(localPath + "/" + name);
ret = newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
My JUnitTest code and TestReport:
@Test
public void testCreateRx() throws Exception { // FAIL!
TestSubscriber<Boolean> testSubscriber = new TestSubscriber<>();
source.createRx("JunitTest_Create_Rx").subscribe(testSubscriber);
testSubscriber.assertNoErrors();
testSubscriber.assertReceivedOnNext(Arrays.asList(Boolean.TRUE)); //PASS!
source.createRx("JunitTest_Create_Rx").subscribe(testSubscriber);
testSubscriber.assertNoErrors();
testSubscriber.assertReceivedOnNext(Arrays.asList(Boolean.FALSE)); //FAIL!
//expected to be [false] (Boolean) but was: [true] (Boolean)
}
@Test
public void testCreateNonRx() { // PASS!
boolean fstRet = source.createNonRx("JunitTest_LocalDataSource_Create_Non_Rx");
assertTrue(fstRet);
boolean secRet = source.createNonRx("JunitTest_LocalDataSource_Create_Non_Rx");
assertFalse(secRet);
}
I am a newbie to RxJava, is there a problem with my code?
Why the call to createRx() return TRUE when I try to create an existing file?
Thanks for any help.