Skip to content

Instantly share code, notes, and snippets.

@RubenZagon
Created April 1, 2022 19:56
Show Gist options
  • Save RubenZagon/c15156a58db4e2597b82b23a90895fe2 to your computer and use it in GitHub Desktop.
Save RubenZagon/c15156a58db4e2597b82b23a90895fe2 to your computer and use it in GitHub Desktop.

Java - Live Templates

Java

Java Test

JUnit 5

  • _test (Add new Java test with Assertj)
<template name="_test" value="@Test&#10;public void $TEST_NAME$ () {&#10;    //GIVEN&#10;&#10;    //WHEN&#10;&#10;    //THEN&#10;    org.assertj.core.api.Assertions.assertThat($ASSERT$).isEqualTo($RESULT_EQUAL$);&#10;}" description="Add new Java test with Assertj" toReformat="false" toShortenFQNames="true" useStaticImport="true">
  <variable name="TEST_NAME" expression="snakeCase(String)" defaultValue="&quot;dummy&quot;" alwaysStopAt="true" />
  <variable name="ASSERT" expression="snakeCase(String)" defaultValue="&quot;false&quot;" alwaysStopAt="true" />
  <variable name="RESULT_EQUAL" expression="snakeCase(String)" defaultValue="&quot;true&quot;" alwaysStopAt="true" />
  <context>
    <option name="JAVA_CODE" value="true" />
  </context>
</template>
  • _test_exception (Add new Java exception test with JUnit5)
<template name="_test_exception" value="@Test&#10;public void $TEST_NAME$ () {&#10;    //GIVEN&#10;&#10;    //WHEN&#10;&#10;    //THEN&#10;    org.junit.jupiter.api.Assertions.assertThrows(NumberFormatException.class, () -&gt; {&#10;        Integer.parseInt(&quot;One&quot;);&#10;    });&#10;}" description="Add new Java exception test with JUnit5" toReformat="false" toShortenFQNames="true">
  <variable name="TEST_NAME" expression="snakeCase(String)" defaultValue="&quot;dummy&quot;" alwaysStopAt="true" />
  <context>
    <option name="JAVA_CODE" value="true" />
  </context>
</template>
  • _test_value_object (Add a battery of default test for value objects with JUnit5 and Assertj)
<template name="_test_value_object" value="@Test&#10;void generate_a_$CLASSNAME$() {&#10;    $END$&#10;    Assertions.assertThat(true).isEqualTo(false);&#10;}&#10;&#10;@Disabled&#10;@Test&#10;void not_allow_empty_values() {&#10;&#10;    org.junit.jupiter.api.Assertions.assertThrows(RuntimeException.class, () -&gt; {&#10;        // TODO add method&#10;    });&#10;}&#10;&#10;@Disabled&#10;@Test&#10;void not_allow_null_values() {&#10;&#10;    org.junit.jupiter.api.Assertions.assertThrows(RuntimeException.class, () -&gt; {&#10;        // TODO add method&#10;    });&#10;}" description="Add a battery of default test for value objects with JUnit5 and Assertj" toReformat="true" toShortenFQNames="true" useStaticImport="true">
  <variable name="CLASSNAME" expression="regularExpression(snakeCase(className()), &quot;_should&quot;, &quot;&quot;)" defaultValue="" alwaysStopAt="true" />
  <context>
    <option name="JAVA_CODE" value="true" />
    <option name="JAVA_COMMENT" value="false" />
  </context>
</template>
  • _test_enum (Add a battery of default test for enum with JUnit5 and Assertj)
<template name="_test_enum" value="private final String VALUE_1 = &quot;VALUE_1&quot;;&#10;private final String VALUE_2 = &quot;VALUE_2&quot;;&#10;private final String INVALID_$CLASS_NAME_UPPER_SNAKE$  = &quot;INVALID_$CLASS_NAME_UPPER_SNAKE$&quot;;&#10;&#10;@Test$END$&#10;void only_be_created_with_a_valid_$CLASS_NAME_LOWER_SNAKE$() {&#10;    org.junit.jupiter.api.Assertions.assertNotNull($OBJECT_NAME$.from(VALUE_1));&#10;    org.junit.jupiter.api.Assertions.assertNotNull($OBJECT_NAME$.from(VALUE_2));&#10;}&#10;&#10;@Disabled&#10;@Test&#10;void fail_when_$CLASS_NAME_LOWER_SNAKE$_is_invalid() {&#10;    org.junit.jupiter.api.Assertions.assertThrows(Invalid$OBJECT_NAME$.class, () -&gt; $OBJECT_NAME$.from(INVALID_$CLASS_NAME_UPPER_SNAKE$));&#10;}" description="Add a battery of default test for enum with JUnit5 and Assertj" toReformat="false" toShortenFQNames="true">
  <variable name="CLASS_NAME_UPPER_SNAKE" expression="regularExpression(capitalizeAndUnderscore(className()), &quot;_SHOULD&quot;, &quot;&quot;)" defaultValue="" alwaysStopAt="true" />
  <variable name="CLASS_NAME_LOWER_SNAKE" expression="regularExpression(snakeCase(className()), &quot;_should&quot;, &quot;&quot;)" defaultValue="" alwaysStopAt="true" />
  <variable name="OBJECT_NAME" expression="regularExpression(className(), &quot;Should&quot;, &quot;&quot;)" defaultValue="" alwaysStopAt="true" />
  <context>
    <option name="JAVA_CODE" value="true" />
    <option name="JAVA_COMMENT" value="false" />
  </context>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment