Add AccountUtil Unit Tests (#4814)

This commit is contained in:
Madhur Gupta 2022-02-15 15:10:13 +05:30 committed by GitHub
parent dcf3aa8670
commit fd0dbf4cfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 120 additions and 0 deletions

View file

@ -0,0 +1,61 @@
package fr.free.nrw.commons.auth
import fr.free.nrw.commons.FakeContextWrapper
import fr.free.nrw.commons.FakeContextWrapperWithException
import fr.free.nrw.commons.TestCommonsApplication
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class)
class AccountUtilUnitTest {
private lateinit var context: FakeContextWrapper
private lateinit var accountUtil: AccountUtil
@Before
@Throws(Exception::class)
fun setUp() {
context = FakeContextWrapper(RuntimeEnvironment.application.applicationContext)
accountUtil = AccountUtil()
}
@Test
@Throws(Exception::class)
fun checkNotNull() {
Assert.assertNotNull(accountUtil)
}
@Test
@Throws(Exception::class)
fun testGetUserName() {
Assert.assertEquals(AccountUtil.getUserName(context), "test@example.com")
}
@Test
@Throws(Exception::class)
fun testGetUserNameWithException() {
val context =
FakeContextWrapperWithException(RuntimeEnvironment.application.applicationContext)
Assert.assertEquals(AccountUtil.getUserName(context), null)
}
@Test
@Throws(Exception::class)
fun testAccount() {
Assert.assertEquals(AccountUtil.account(context)?.name, "test@example.com")
}
@Test
@Throws(Exception::class)
fun testAccountWithException() {
val context =
FakeContextWrapperWithException(RuntimeEnvironment.application.applicationContext)
Assert.assertEquals(AccountUtil.account(context), null)
}
}