Skip to content

lesson2 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,13 @@ dependencies {
implementation 'com.google.dagger:dagger:2.42'
kapt 'com.google.dagger:dagger-compiler:2.42'

//Mockito
testImplementation "org.mockito:mockito-core:3.12.4"
testImplementation 'org.mockito:mockito-inline:2.8.9'
testImplementation('com.nhaarman:mockito-kotlin:1.5.0') {
exclude group: 'org.jetbrains.kotlin'
exclude group: 'org.mockito'
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MainPresenter : MvpPresenter<MainView>() {

@Inject
lateinit var screens: IScreens
override fun onFirstViewAttach() {
public override fun onFirstViewAttach() {
super.onFirstViewAttach()
router.replaceScreen(screens.users())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RepositoryPresenter(private val githubUserRepositories: GithubUserReposito
@Inject
lateinit var router: Router

override fun onFirstViewAttach() {
public override fun onFirstViewAttach() {
super.onFirstViewAttach()
viewState.init()
githubUserRepositories?.let { viewState.setId(it.id) }
Expand Down
59 changes: 59 additions & 0 deletions app/src/test/java/com/example/githubclient/MainPresenterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.example.githubclient

import com.example.githubclient.mvp.presenter.MainPresenter
import com.example.githubclient.mvp.view.MainView
import com.example.githubclient.navigation.IScreens
import com.github.terrakok.cicerone.Router
import com.github.terrakok.cicerone.Screen
import com.nhaarman.mockito_kotlin.atLeastOnce
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations

class MainPresenterTest {
private lateinit var presenter: MainPresenter

@Mock
private lateinit var view: MainView

@Mock
private lateinit var router: Router

@Mock
private lateinit var screens: IScreens

@Before
fun setUp() {
MockitoAnnotations.openMocks(this)

presenter = MainPresenter()
presenter.router = router
presenter.screens = screens
presenter.attachView(view)

}

@Test
fun onFirstViewAttach_Test() {
val expectedScreen = mock<Screen>()
whenever(screens.users()).thenReturn(expectedScreen)

presenter.onFirstViewAttach()

Mockito.verify(screens, atLeastOnce()).users()
Mockito.verify(router, times(1)).replaceScreen(expectedScreen)
}

@Test
fun backClicked_shouldExitRouter() {

presenter.backClicked()

Mockito.verify(router, times(1)).exit()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example.githubclient

import com.example.githubclient.mvp.model.entity.GithubUserRepositories
import com.example.githubclient.mvp.presenter.RepositoryPresenter
import com.example.githubclient.mvp.view.RepositoryView
import com.github.terrakok.cicerone.Router
import com.nhaarman.mockito_kotlin.atMost
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations

class RepositoryPresenterTest {
private lateinit var presenter: RepositoryPresenter

@Mock
private lateinit var view: RepositoryView

@Mock
private lateinit var router: Router

@Mock
private lateinit var githubRepository: GithubUserRepositories

@Before
fun setUp() {
MockitoAnnotations.openMocks(this)

githubRepository = mock()
presenter = RepositoryPresenter(githubRepository)
presenter.router = router
presenter.attachView(view)
}

@Test
fun onFirstViewAttach_Test() {
whenever(githubRepository.id)
.thenReturn("1")
.thenReturn("2")
whenever(githubRepository.name)
.thenReturn("Ololo")
.thenReturn("Ururu")
whenever(githubRepository.forksCount)
.thenReturn(5)
.thenReturn(10)

presenter.onFirstViewAttach()

verify(githubRepository, atMost(2)).id
verify(githubRepository, atMost(2)).name
verify(githubRepository, atMost(2)).forksCount
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

еще детач можно протестировать


@Test
fun backPressed_Test() {
presenter.backPressed()

verify(router, times(1)).exit()
}
}