Created
July 9, 2019 14:26
-
-
Save lman/6ff15c951f2d43c16575d6c9c37d77e2 to your computer and use it in GitHub Desktop.
Example of toothpick junit5+mockito test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCalendarPresenter @Inject constructor(private val view: IMycalendarView, | |
private val repository: IUserMemoriesRepository) { | |
fun onAttach() { | |
view.showUserMemories(getMemories()) | |
} | |
fun getMemories(): String { | |
return "memories" | |
} | |
} | |
// ---------------------------- | |
interface IUserMemoriesRepository { | |
fun getUserMemories(): String | |
} | |
// ---------------------------- | |
class MyCalendarController : Controller(), IMycalendarView { | |
@Inject | |
lateinit var presenter: MyCalendarPresenter | |
private val diHelper = DiHelper(this, Scopes.MY_CALENDAR_SCOPE) | |
.init { it.installModules(MyCalendarModule(this)) } | |
override fun onCreateView(@NonNull inflater: LayoutInflater, @NonNull container: ViewGroup): View { | |
return inflater.inflate(R.layout.controller_my_calendar, container, false) | |
} | |
override fun onAttach(view: View) { | |
super.onAttach(view) | |
presenter.onAttach() | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
diHelper.close() | |
} | |
override fun showUserMemories(txt: String) { | |
view?.txt?.text = txt | |
} | |
} | |
// ---------------------------- | |
@ExtendWith(MockitoExtension::class) | |
@MockitoSettings(strictness = Strictness.LENIENT) | |
class MyCalendarPresenterTest { | |
@RegisterExtension val toothPickExtension: ToothPickExtension = ToothPickExtension(this, "Foo") | |
@Mock | |
lateinit var view: IMycalendarView | |
@Mock | |
lateinit var userMemoriesRepository: IUserMemoriesRepository | |
lateinit var presenter: MyCalendarPresenter | |
@Test | |
fun onAttach_Ok() { | |
//1 | |
val memories = "memories" | |
//2 | |
whenever(userMemoriesRepository.getUserMemories()).thenReturn(memories) | |
//3 | |
presenter = toothPickExtension.getInstance(MyCalendarPresenter::class.java) | |
presenter.onAttach() | |
//4 | |
verify(view).showUserMemories(eq(memories)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment