How can I have newly created JUnit tests based on a template?

Ctrl-Shift-T creates me a new test - fantastic.

But - how can I change how that new test is created? In my newly created JUnit 4 tests, I'd really like to have imports for org.junit.Assert.*. I'd also really like to have imports for the Hamcrest matcher packages.

Is there any way of doing this?

Cheers

Inigo

3 comments
Comment actions Permalink

Ah - to answer my own question, it looks like you can't: it's issue http://youtrack.jetbrains.net/issue/IDEA-21205.

Oh well.

0
Comment actions Permalink

It seems that IDEA uses the 'new class' template for generated tests.  I thought adding

#if ($NAME.endsWith("Test"))
import static org.junit.Assert.*;
#end

to the template might work... It adds the extra import to new classes that end in "Test".

0
Comment actions Permalink

I do this using a file template below is my "Basic JUnit4 Test" template:

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
#parse("File Header.java")
public class ${NAME} {
    @BeforeClass
    public static void beforeClass() throws Exception {
    }
    @Before
    public void before() {
    }
    @After
    public void after() {
    }
    
    @Test
    public void basicTest() throws Exception {
        
    }
}

0

Please sign in to leave a comment.