Any way to make a LiveTemplate do what I want?

I'm trying to make a Live template to help me write jUnit test cases.

Most of my test cases are of the form:



I've tried pretty extensively to make a template for this. My current best attempt looks like:



And I have my variables set up as:


This doesn't do what I want really though, because the rightSideType() call is just blank. If I try to put a default of "Object" for the TYPE variable, IntelliJ throws a NullPointerException. Any ideas how to fix this? It would sure make my day.

Thanks!

7 comments

I have VARNAME down in the bottom table where I meant just NAME. That's just a typo here, my actual template uses the correct variable name consistantly. :)

0

Hi there, anyone have a chance to look at this?

I'm beginning to think this might be an IntelliJ bug and would just like confirmation that I'm not trying to do something stupid or assuming something should work that isn't supposed to.

Thanks :)

0

At the third line it should be $EXPECTED$.
The rightSideType can only return stuff when "someClass" is an
known class.
Maybe you also need an variable for that?

Aaron Cohen schrieb:

I'm trying to make a Live template to help me write jUnit test cases.

Most of my test cases are of the form:

            SomeType attribute = someClass.getAttribute();
>            SomeType expectedAttribute = new Object();  // Replace with test case
>            Assert.assertEquals(expectedAttribute, expectedAttribute);
>     }]]>



I've tried pretty extensively to make a template for this. My current best attempt looks like:

           $TYPE$ $LCASENAME$ = someClass.get$TEST$();
>           $TYPE$ expected$NAME$ = $EXPECTED;
>           junit.framework.Assert.assertEquals(expected$NAME$, $LCASENAME$);
>     }]]>



And I have my variables set up as:
Name | Expression | Default Value | Skip if defined > TEST | complete() | | No > TYPE | rightSideType() | | Yes > LCASENAME | decapitalize(VARNAME) | | Yes > VARNAME | TEST | | Yes > EXPECTED | | "new Object()" | No > > ]]>

This doesn't do what I want really though, because the rightSideType() call is just blank. If I try to put a default of "Object" for the TYPE variable, IntelliJ throws a NullPointerException. Any ideas how to fix this? It would sure make my day.

Thanks!

0

Don't you forget () after method name?

0

Thanks for the response, the error on EXPECTED was just my typo while typing it here in the web page.

As far as "someClass", that is always the same class so I just put have it hard-coded in the template (I create the "someClass" in the setUp function of my testSuite).

Since $TYPE$ comes after $TEST$ in my variable list, I expected it to wait until I select a method from the completion box before trying to make the call to rightSideType(), but instead the whole template just blows up with a NullPointerException before I can even do the completion.

0

Ah you're right, but unfortunately fixing it doesn't change anything whatsoever, still the exact same problem.

Since $TYPE$ comes after $TEST$ in my variable list, I expect it to wait until I select a method from the completion box before trying to make the call to rightSideType(), but instead the whole template just blows up with a NullPointerException before I can even do the completion.

0

I don't see a NPE but there is no suggestion for $TYPE$
Seems you need some more functions. When using you template
there is no suggestion for rightSideType() because the partial
code is no valid (no variable name). Adding an prefix for the
current value (like your expectedXXX) does also not work because
of the missing type... You know, chicken and egg :)
If there would be:
typeOf(expr) (like componentTypeOf)
nameOf(expr)
one could write:

public void test$NAME$() {
$TYPE$ $LCASENAME$ = getData().get$TEST$;
$TYPE$ expected$NAME$ = $EXPECTED$;
junit.framework.Assert.assertEquals(expected$NAME$, $LCASENAME$);
}

with
TEST: complete()
NAME: nameOf(TEST)
TYPE: typeOf(TEST)
LCASENAME: decapitalize(NAME)
EXPECTED:

Aaron Cohen schrieb:

Thanks for the response, the error on EXPECTED was just my typo while typing it here in the web page.

As far as "someClass", that is always the same class so I just put have it hard-coded in the template (I create the "someClass" in the setUp function of my testSuite).

Since $TYPE$ comes after $TEST$ in my variable list, I expected it to wait until I select a method from the completion box before trying to make the call to rightSideType(), but instead the whole template just blows up with a NullPointerException before I can even do the completion.

0

Please sign in to leave a comment.