- Create a new test class:
class MyTest {
}
- Create a mocked instance of the RegistrationApi interface as the test-class property:
class MyTest {
private val api = mock<RegistrationApi>()
}
- Add a class property of the RegistrationFormController type:
class MyTest {
private val api = mock<RegistrationApi>()
private var registrationFormController =
RegistrationFormController(api = api)
}
- Create the test method to check whether checkIfEmailCanBeRegistered() behaves correctly for an invalid email address occurrence:
class MyTest {
private val api = mock<RegistrationApi>()
private lateinit var registrationFormController: RegistrationFormController
@Before
fun setup() {
registrationFormController = RegistrationFormController(api = api)
}
@Test
fun `email shouldn't be registered if it's not valid`() {
// given
assertNotNull(registrationFormController)
whenever(api.isEmailAddressAvailable(anyString())) doReturn(true)
// when
registrationFormController.currentEmailAddress = "Hilary"
// then
assertFalse(registrationFormController.checkIfEmailCanBeRegistered())
}
}