Skip to content

Commit

Permalink
Now the schedulers are passed through constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
saulmm committed Dec 21, 2015
1 parent 6dd2b7d commit 0ab9a4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ public class GetCharactersUsecase extends Usecase<List<MarvelCharacter>> {
private final CharacterRepository mRepository;
private int mCurrentOffset;

@Inject @Named("ui_thread") Scheduler uiThread;
@Inject @Named("executor_thread") Scheduler executorThread;
Scheduler uiThread, executorThread;


@Inject public GetCharactersUsecase(CharacterRepository repository) {
@Inject public GetCharactersUsecase(CharacterRepository repository, @Named("ui_thread") Scheduler uiThread, @Named("executor_thread") Scheduler executorThread) {
mRepository = repository;
this.uiThread = uiThread;
this.executorThread = executorThread;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion domain/src/test/java/GetCharacterDetailsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class GetCharacterDetailsTest {
}

private CharacterDetailsUsecase givenACharacterUsecase() {
return new CharacterDetailsUsecase(FAKE_CHARACTER_ID, mRepository, mockScheduler, mockScheduler);
return new CharacterDetailsUsecase(FAKE_CHARACTER_ID, mRepository);
}

private Observable<MarvelCharacter> getFakeCharacterObservable() {
Expand Down
23 changes: 13 additions & 10 deletions domain/src/test/java/GetCharactersUsecaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.when;
import static saulmm.avengers.GetCharactersUsecase.DEFAULT_CHARACTERS_LIMIT;

public class GetCharactersUsecaseTest {
@Mock CharacterRepository mockRepository;
@Mock Scheduler mockUiScheduler;
@Mock Scheduler mockExecutorScheduler;

@Before public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
Expand All @@ -36,26 +39,26 @@ public class GetCharactersUsecaseTest {

@Test public void testThatCharactersUsecaseIncrementsOffset() throws Exception {
GetCharactersUsecase charactersUsecase = givenACharactersUsecase();

when(mockRepository.getCharacters(anyInt())).thenReturn(getFakeObservableCharacterList());
charactersUsecase.executeIncreasingOffset();

assertThat(charactersUsecase.getCurrentOffset(), is(DEFAULT_CHARACTERS_LIMIT));
charactersUsecase.execute();
charactersUsecase.execute();
charactersUsecase.execute();

assertThat(charactersUsecase.getCurrentOffset(), is(DEFAULT_CHARACTERS_LIMIT * 3));
}

@Test public void testThatCharactersUsecaseWithOffsetIsCalledOnce() throws Exception {
GetCharactersUsecase charactersUsecase = givenACharactersUsecase();
int fakeCurrentOffset = 20;
when(mockRepository.getCharacters(anyInt())).thenReturn(getFakeObservableCharacterList());

charactersUsecase.execute();

when(mockRepository.getCharacters(fakeCurrentOffset)).thenReturn(getFakeObservableCharacterList());
charactersUsecase.executeIncreasingOffset();

Mockito.verify(mockRepository, only()).getCharacters(fakeCurrentOffset);
Mockito.verify(mockRepository, only()).getCharacters(anyInt());
}

private GetCharactersUsecase givenACharactersUsecase() {
return new GetCharactersUsecase(mockRepository,
Schedulers.io(), Schedulers.newThread());
return new GetCharactersUsecase(mockRepository, mockUiScheduler, mockExecutorScheduler);
}

private Observable<List<MarvelCharacter>> getFakeObservableCharacterList() {
Expand Down

0 comments on commit 0ab9a4f

Please sign in to comment.