Mike
Mike

Reputation: 19

JUnit in Intellij Idea generates empty test class body

Everytime I create a JUnit 5 test class in IntelliJ IDEA (Ctrl+Shift+T) on my class, it generates blank test fixture with an empty method body.

e.g.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class RegTest {

    @Test
    void insert() {
    }

    @Test
    void delete() {
    }
}

If I write into these methods assertions by myself, it works fine.

Shouldn't it prepare (generate) for instance valuables expResult, data, or assertEquals(expResult,data)?

Sorry to bother you, but I read JUnit FAQ and IntelliJ IDEA help and still am without clue.

Thank you

Upvotes: 1

Views: 975

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26572

Under Editor -> File and Code Templates, you can define a template for a Java class (general).

Assuming that all your test classes end with Test, then you could edit that general template with the following:

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

#if ($NAME.endsWith("Test"))
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
#end
#parse("File Header.java")
public class ${NAME} {

     @Test
     public void testTemplate() throws Exception{
         // Arrange


        // Act


        // Assert
     }       
}

in order to get the basic unit testing imports and have a ready template for your first test.

Upvotes: 4

Related Questions