Created
July 31, 2021 15:28
-
-
Save Iannnr/7e5ad0aa6626166e829cedfc3b69c6bb to your computer and use it in GitHub Desktop.
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
@RunWith(RobolectricTestRunner::class) | |
class ExampleWorkerTest { | |
@Mock | |
lateinit var api: ApiInterface | |
lateinit var context: Context | |
@Before | |
fun setup() { | |
MockitoAnnotations.openMocks(this) | |
context = ApplicationProvider.getApplicationContext() | |
} | |
@Test | |
fun `assert that when input data is true then result is success`() { | |
runBlocking { | |
`when`(api.postData(PostData(true))).thenReturn(Response.success("".toResponseBody())) | |
val repo = Repository(api) | |
val worker = TestListenableWorkerBuilder<ExampleWorker>(context) | |
.setWorkerFactory(ExampleWorkerFactory(repo)) | |
.setInputData( | |
workDataOf("data" to true) | |
) | |
.build() | |
val result = worker.doWork() | |
assertThat(result, `is`(ListenableWorker.Result.success())) | |
} | |
} | |
@Test | |
fun `assert that when input data is false then result is unsuccessful`() { | |
runBlocking { | |
`when`(api.postData(PostData(false))).thenReturn(Response.error(400, "".toResponseBody())) | |
val repo = Repository(api) | |
val worker = TestListenableWorkerBuilder<ExampleWorker>(context) | |
.setWorkerFactory(ExampleWorkerFactory(repo)) | |
.setInputData( | |
workDataOf("data" to false) | |
) | |
.build() | |
val result = worker.doWork() | |
assertThat(result, `is`(ListenableWorker.Result.failure())) | |
} | |
} | |
class ExampleWorkerFactory(private val repo: Repository) : WorkerFactory() { | |
override fun createWorker(appContext: Context, workerClassName: String, workerParameters: WorkerParameters): ListenableWorker? { | |
return ExampleWorker(appContext, workerParameters, repo) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment