Code template - generate variable name

Answered

I want to create unit tests with Mockito as base on my project. For this, I want to set up a code template that generates all boilerplate code. I've succeeded in setting up most of the code generation, but I want to have dynamically named variable names for the tested classes. So, when I'm creating a test class for the class TestClass, I want the corresponding variable to be named testClass (basically just decapitalize the first symbol of the TestClass).

This is my current setup:

0
8 comments

That's for live templates. I'm trying to create a code template.

There is a checkbox for enabling live templates, but I don't see how it works. I'm not getting any options to edit template variables as when working with live templates.

I also can't just write "decapitalize()" right into the template, as it will just write that in plain text when I create the test class.

0

Live template text should be escaped using #[[ ... ]]# see Enable Live Templates check-box description at File and Code Templates reference.

0

This doesn't seem to work. Trying to set up a new variable $VAR$ did nothing. Am I supposed get a dialog window that allows me to set variables, as in live templates? I tried this by escaping both $VAR$ and decapitalize(ClassName).

 

 

 

0

Please share your template as a text, I'll try to reproduce that locally. 

0
import ${CLASS_NAME};

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

#parse("File Header.java")
@RunWith(MockitoJUnitRunner.class)
@Tag("unit")
class ${NAME} {

@InjectMocks
private ${CLASS_NAME} #[[ decapitalize(ClassName) ]]#;

@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
}

${BODY}
}
0

Sorry, it does not work for Java. Here is the related bug request: 

https://youtrack.jetbrains.com/issue/IDEA-159930/Live-template-variables-dont-work-in-Includes-and-Code-templates

I do not see a way to add decapitalize variables right now. The only workaround is to insert the class name as usual and replace the Upper case letters manually. 

 

1

I see. Thanks for taking time to look into this.

0

Please sign in to leave a comment.