BAEL-5908: Single.just vs fromCallable
This commit is contained in:
parent
0484640a06
commit
f8a341aaa8
5
rxjava-modules/rxjava-core-2/README.md
Normal file
5
rxjava-modules/rxjava-core-2/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
## RxJava
|
||||||
|
|
||||||
|
This module contains articles about RxJava.
|
||||||
|
|
||||||
|
### Relevant articles:
|
16
rxjava-modules/rxjava-core-2/pom.xml
Normal file
16
rxjava-modules/rxjava-core-2/pom.xml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>rxjava-core-2</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>rxjava-core-2</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.rxjava-modules</groupId>
|
||||||
|
<artifactId>rxjava-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.rxjava.justvscallable;
|
||||||
|
|
||||||
|
public interface EmployeeRepository {
|
||||||
|
String findById(Long id);
|
||||||
|
}
|
13
rxjava-modules/rxjava-core-2/src/main/resources/logback.xml
Normal file
13
rxjava-modules/rxjava-core-2/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.baeldung.rxjava.justvscallable;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.reset;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import rx.Single;
|
||||||
|
import rx.observers.TestSubscriber;
|
||||||
|
|
||||||
|
class EmployeeRepositoryTest {
|
||||||
|
|
||||||
|
public EmployeeRepository repository = mock(EmployeeRepository.class);
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void beforeEach() {
|
||||||
|
reset(repository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNoSubscriber_whenUsingJust_thenDataIsFetched() {
|
||||||
|
Mockito.when(repository.findById(123L))
|
||||||
|
.thenReturn("John Doe");
|
||||||
|
|
||||||
|
Single<String> employee = Single.just(repository.findById(123L));
|
||||||
|
|
||||||
|
Mockito.verify(repository, times(1))
|
||||||
|
.findById(123L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenASubscriber_whenUsingJust_thenReturnTheCorrectValue() {
|
||||||
|
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
|
||||||
|
Mockito.when(repository.findById(123L))
|
||||||
|
.thenReturn("John Doe");
|
||||||
|
|
||||||
|
Single<String> employee = Single.just(repository.findById(123L));
|
||||||
|
employee.subscribe(testSubscriber);
|
||||||
|
|
||||||
|
testSubscriber.assertValue("John Doe");
|
||||||
|
testSubscriber.assertCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNoSubscriber_whenUsingFromCallable_thenNoDataIsFetched() {
|
||||||
|
Single<String> employee = Single.fromCallable(() -> repository.findById(123L));
|
||||||
|
|
||||||
|
Mockito.verify(repository, never())
|
||||||
|
.findById(123L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenASubscriber_whenUsingFromCallable_thenReturnCorrectValue() {
|
||||||
|
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
|
||||||
|
Mockito.when(repository.findById(123L))
|
||||||
|
.thenReturn("John Doe");
|
||||||
|
|
||||||
|
Single<String> employee = Single.fromCallable(() -> repository.findById(123L));
|
||||||
|
employee.subscribe(testSubscriber);
|
||||||
|
|
||||||
|
Mockito.verify(repository, times(1))
|
||||||
|
.findById(123L);
|
||||||
|
testSubscriber.assertCompleted();
|
||||||
|
testSubscriber.assertValue("John Doe");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user