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

2
5 comments

You can change it here:

0
Avatar
Permanently deleted user

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.

1
Avatar
Permanently deleted user

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.  

0

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:

2

Please sign in to leave a comment.