ผลต่างระหว่างรุ่นของ "Sw-spec/unit-test-and-mock"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 1: | แถว 1: | ||
== Links == | == Links == | ||
+ | |||
+ | * MVP documents: TBA | ||
+ | * [http://site.mockito.org/ Mockito] | ||
== Codes == | == Codes == |
รุ่นแก้ไขเมื่อ 04:21, 23 มีนาคม 2561
Links
- MVP documents: TBA
- Mockito
Codes
CounterView.kt
interface CounterView { fun setCounter(value: Int) }
CounterPresenter.kt
class CounterPresenter(val view: CounterView) { var counter: Int = 0 fun onPlusButtonClicked() { counter++ updateCounterInView() } fun onMinusButtonClicked() { counter-- updateCounterInView() } fun onResetButtonClicked() { reset() } fun start() { reset() } private fun reset() { counter = 0 updateCounterInView() } private fun updateCounterInView() { view.setCounter(counter) } }
CounterPresenterTest.kt
import org.junit.Before import org.junit.Test import org.mockito.Mockito.* /** * Created by jittat on 23/3/2561. */ class CounterPresenterTest { lateinit var view: CounterView lateinit var presenter: CounterPresenter @Before fun init() { view = mock(CounterView::class.java) presenter = CounterPresenter(view) } @Test fun testResetCounter() { presenter.start() verify(view).setCounter(0) } @Test fun testAddCounter() { presenter.start() presenter.onPlusButtonClicked() val inOrder = inOrder(view) inOrder.verify(view).setCounter(0) inOrder.verify(view).setCounter(1) } }