I am trying to create a new file using RxJava on Android, for example:
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 in normal mode, follow these steps:
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 code is JUnitTest and TestReport:
@Test
public void testCreateRx() throws Exception {
TestSubscriber<Boolean> testSubscriber = new TestSubscriber<>();
source.createRx("JunitTest_Create_Rx").subscribe(testSubscriber);
testSubscriber.assertNoErrors();
testSubscriber.assertReceivedOnNext(Arrays.asList(Boolean.TRUE));
source.createRx("JunitTest_Create_Rx").subscribe(testSubscriber);
testSubscriber.assertNoErrors();
testSubscriber.assertReceivedOnNext(Arrays.asList(Boolean.FALSE));
}
@Test
public void testCreateNonRx() {
boolean fstRet = source.createNonRx("JunitTest_LocalDataSource_Create_Non_Rx");
assertTrue(fstRet);
boolean secRet = source.createNonRx("JunitTest_LocalDataSource_Create_Non_Rx");
assertFalse(secRet);
}
I'm new to RxJava, is there a problem with my code?
Why does the createRx () call return TRUE when trying to create an existing file?
Thanks for any help.
source
share