How to create template for Kotlin & JUnit test
Answered
Hi,
I change a little bit IntelliJ Code Template for "JUnit4 Test Method" to :
@org.junit.Test
public void ${NAME}() {
// given
${BODY}
// when
// then
}
However, it does not work correctly with Kotlin generating test like:
@Test
fun `test name`() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
Could you help me to define properly this Code Template or suggest the different approach?
Best Regards,
Sławek
Please sign in to leave a comment.
You can change it here:
After your suggestion, all the newly generated functions body has content as in template, also the production code (I do not need in a production function body: given,when,then). Is it possible to make that template work only for test methods?
I want to only add that when I use Java, Code Template created by me generate function body properly.
Request is welcome at https://youtrack.jetbrains.com/issues/KT.
Hi, we just switched to Kotlin from Java for a new project and I realise that it's not possible use custom templates for Kotlin test methods, the request is created https://youtrack.jetbrains.com/issue/KT-46704. Thanks.
The fact that this is still a limitation in 2021 is disappointing and discouraging, to say the least.
One workaround I found is to create a live template. Something like this: