Skip to content

IDEA Plugin for creation Boilerplate test code for Mockk

License

Notifications You must be signed in to change notification settings

Vladuken/mockk-test-generator

Repository files navigation

IDEA Plugin that generates boilerplate code for Mockk tests.

Description:

Plugin take class as an input:
package com.test.project

class Foo(
    val t1: Any,
    val t2: (Any) -> Unit,
    val t3: kotlin.Lazy<Any>,
    val t4: dagger.Lazy<Any>,
)
And generates test class with some boilerplate code as output:
package com.test.project

import io.mockk.impl.annotations.MockK

class FooTest {

    @MockK
    lateinit var t1: Any

    @MockK
    lateinit var t2: (Any) -> Unit

    @MockK
    lateinit var t3: Any

    @MockK
    lateinit var t4: Any

    private fun prepareFoo(
        t1: Any = this.t1,
        t2: (Any) -> Unit = this.t2,
        t3: Any = this.t3,
        t4: Any = this.t4,
    ): Foo {
        return Foo(
            t1 = t1,
            t2 = t2,
            t3 = lazyOf(t3),
            t4 = { t4 },
        )
    }

}

How To Use

Plugin is a part of Generate group - open it with next shortcuts and select Generate MockK Test:

MacOS: Cmd + N

Windows: Alt + Insert

mockk-generate.png

You will see next dialog:

usage-1.png

Option Description
1 Should generate prepareMethod() as Extension of TestScope Create method as Ext of TestScope
2 Should relaxUnitFun @Mockk-ed fields Will add relaxUnitFun to every @Mockk field
3 Should relax @Mockk-ed fields Will add relax to every @Mockk field
4 Should generate @Before method Will generate @Before method and mock annotated with @MockK fields
5 Should generate @After method Will generate @After method and un-mock everything

Live Templates - How to Use

Plugin contains some preinstalled Live Templates that will simplify test writing. Each of templates is context aware and most of them are permitted to be called mostly in Test files.

List of supported Live Templates

fb
@Before
fun before() {
    MockKAnnotations.init(this)
}
fa
@After
fun after() {
}
ft
@Test
fun `test {TEST NAME PLACEHOLDER}`() {
    // given
    // when
    // then
}
gi
// given
wh
// when
th
// then
re
// region {REGION_DESCRIPTION}
end
// endregion

How To Install

  • Download mockk-test-generator-VERSION.zip from Releases section

installation-1.png

  • Open Plugins settings screen

installation-2.png

  • Select Install Plugin from Disk...

installation-3.png

  • Select mockk-test-generator-VERSION.zip file.
  • Restart IDE if needed.

About

IDEA Plugin for creation Boilerplate test code for Mockk

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages