Uncategorized

Mocking and stubbing suspend functions with MockK

Prerequisites Lets suppose that in our system we have dependency on use case with suspend method: interface ProvideCurrentUser { suspend fun execute(): UserDetails}data class UserDetails(val id: String, val name: String) This use case is responsible for resolving user currently logged in to app. We have also dependency on repository, also with suspend function getUserById: interface …

Mocking and stubbing suspend functions with MockK Read More »

Intro to testing Ktor controllers

Let’s suppose that we want to dismiss all calls for api/restricted endpoint if they don’t have special header, in this case „X-Special-Header”. We access header via get PipelineContext  and check if that value is null or blank. If so, we respond with HTTP 403. If given header is present,  we proceed with some business logic …

Intro to testing Ktor controllers Read More »

Using Mockito in Kotlin projects

We will work on the following piece of code. It’s simplified version of Presenter in Model-View-Presenter pattern. Let’s see what we can and should unit test there. interface View { fun displayItems(list: List<Element>) fun displayDetails(element: Element) fun displayError()}data class Element(val id: Int, val content: String)interface DataProvider { fun getAll(): List<Element> fun getOne(id: Int): Element?}class Presenter( …

Using Mockito in Kotlin projects Read More »

Testing Retrofit calls with OkHttp MockWebServer

Retrofit – probably the most popular networking client in Android development. Basically it allows to create HTTP client in an interface – you just add annotation with HTTP method, relative or absolute path and proper request is constructed. Retrofit does not generate code in compile time – it creates implementations in runtime. import okhttp3.ResponseBodyimport retrofit2.Callimport …

Testing Retrofit calls with OkHttp MockWebServer Read More »

An attempt to unit test generated DataBinding code

But why? I use databinding a lot in my Android projects. It fits nicely with ViewModel and general MVVM approach. I also make use of databinding in small layouts – such as RecyclerView row or reusable <include> layouts. How does databinding works in general? You enable special build feature in build.gradle: android { (…) buildFeatures …

An attempt to unit test generated DataBinding code Read More »

Testing time-based code with Joda Time

In some systems you sometimes need to record date or timestamp of given action. Good example could be comments system, where each comment has timestamp: interface CommentsInteractor { fun createComment(content: String): CommentEntity}data class CommentEntity( val content: String, val timestamp: Long) In our experiment we will perform two test cases: comment created at given time should …

Testing time-based code with Joda Time Read More »

Parameterized tests with Kotest

We make use of parameterized test when we want to check system under test against various inputs while using same or similar test logic. Real life example for parameterized test may be unit conversion – meters to kilometers converter for the sake of UI display. In some place in an application (either web or mobile) …

Parameterized tests with Kotest Read More »

Handling exceptions in tests: Junit & Kotest

Exceptions and throwables are essential part of many Java APIs. They are useful for modeling application domain layer and controlling program execution flow. We’ll be working on following piece of code: class CustomException : Exception()class SystemUnderTest { fun doStuff() { throw CustomException() }} Annotated Junit4 method class AssertionsOnExceptionsTest { @Test(expected = CustomException::class) fun `it should …

Handling exceptions in tests: Junit & Kotest Read More »

Test execution basics: passed, failed, crashed

In Junit based testing framework there are few possible test outcomes: passed, failed and crashed. In this short note we will dive into that mechanisms. Test will pass, if no exception is thrown. Empty test should always pass: class TestFlowShowcase : StringSpec({ "it should pass"{ // nothing here }}) Or test with assertion that passes: …

Test execution basics: passed, failed, crashed Read More »