JUnit templates:
setup (Context Java type members) Used to create a JUnit4 setUp method
${:import(org.junit.Before)}
@Before
public void setUp() {
}
setup_before_class (Context Java type members) Used to create a static JUnit4 Class setUp method
${:import(org.junit.BeforeClass)}
@BeforeClass
public static void setUpBeforeClass() {
}
teardown (Context Java type members) Used to create a JUnit4 tearDown method
${:import(org.junit.After)}
@After
public void tearDown() {
}
teardown_after_class (Context Java type members) Used to create a static JUnit4 Class tearDowan method
${:import(org.junit.AfterClass)}
@AfterClass
public static void tearDownAfterClass() {
}
Test_expected_exception (Context Java type members) Used to create a JUnit4 test method that expects an Exception to be thrown
@${testType:newType(org.junit.Test)}(expected = ${cursor})
public void ${testname}() throws Exception {
${staticImport:importStatic('org.junit.Assert.*')}
}
Test_timeout (Context Java type members) Used to create a JUnit4 test method with a time limit
@${testType:newType(org.junit.Test)}(timeout = ${cursor})
public void ${testname}() throws Exception {
${staticImport:importStatic('org.junit.Assert.*')}
}
JMock templates:
run_with_jmock (Context Java) Used to annotate a Test Class that it should be run with the JMock runner
${:import(org.junit.runner.RunWith,org.jmock.integration.junit4.JMock)}
@RunWith(JMock.class)
mockery_for_classes (Context Java type members) Used to create a new JMock mockery for Classes
${:import(org.jmock.Mockery,org.jmock.lib.legacy.ClassImposteriser)}
private final Mockery mockery = new Mockery() {
{
setImposteriser(ClassImposteriser.INSTANCE);
}
};
mockery_for_interfaces (Context Java type members) Used to create a new JMock mockery for Interfaces
${:import(org.jmock.Mockery)}
private final Mockery mockery = new Mockery();
checking (Context Java statements) Used to setup JMock expectations
${:import(org.jmock.Expectations)}
mockery.checking(new Expectations() {
{
}
});
General templates:
sysdebug (Context Java statements) Slightly more robust than sysout, labels the value printed to standard out.
System.out.println("${word_selection}=["+${word_selection}${}+"].");${cursor}
templates.xml
You can also copy the following text to a templates.xml file and import it through Eclipse Window -> Preferences -> Java -> Editor -> Templates
<?xml version="1.0" encoding="UTF-8"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="Setup JMock expectations" enabled="true" name="checking">${:import(org.jmock.Expectations)}
mockery.checking(new Expectations() {
{
}
});</template><template autoinsert="true" context="java-members" deleted="false" description="Create a new JMock Mockery" enabled="true" name="mockery_for_classes">${:import(org.jmock.Mockery,org.jmock.lib.legacy.ClassImposteriser)}
private final Mockery mockery = new Mockery() {
{
setImposteriser(ClassImposteriser.INSTANCE);
}
};</template><template autoinsert="true" context="java-members" deleted="false" description="Create a new JMock Mockery" enabled="true" name="mockery_for_interfaces">${:import(org.jmock.Mockery)}
private final Mockery mockery = new Mockery();</template><template autoinsert="true" context="java" deleted="false" description="Creates a @RunWith(JMock.class) declaration" enabled="true" name="run_with_jmock">${:import(org.junit.runner.RunWith,org.jmock.integration.junit4.JMock)}
@RunWith(JMock.class)</template><template autoinsert="true" context="java-members" deleted="false" description="setup a JUnit test case" enabled="true" name="setup">${:import(org.junit.Before)}
@Before
public void setUp() {
}</template><template autoinsert="true" context="java-members" deleted="false" description="Create a static setup method" enabled="true" name="setup_before_class">${:import(org.junit.BeforeClass)}
@BeforeClass
public static void setUpBeforeClass() {
}</template><template autoinsert="true" context="java-statements" deleted="false" description="print to standard out with a descriptive variable name" enabled="true" name="sysdebug">System.out.println("${word_selection}=["+${word_selection}${}+"].");${cursor}</template><template autoinsert="true" context="java-members" deleted="false" description="tear down a JUnit test case" enabled="true" name="teardown">${:import(org.junit.After)}
@After
public void tearDown() {
}</template><template autoinsert="true" context="java-members" deleted="false" description="Create a static teardown method" enabled="true" name="teardown_after_class">${:import(org.junit.AfterClass)}
@AfterClass
public static void tearDownAfterClass() {
}</template><template autoinsert="false" context="java-members" deleted="false" description="Test method (JUnit 4) for an expected exception" enabled="true" name="Test_expected_exception">@${testType:newType(org.junit.Test)}(expected = ${cursor})
public void ${testname}() throws Exception {
${staticImport:importStatic('org.junit.Assert.*')}
}</template><template autoinsert="false" context="java-members" deleted="false" description="Test method (JUnit 4) that sets a timeout" enabled="true" name="Test_timeout">@${testType:newType(org.junit.Test)}(timeout = ${cursor})
public void ${testname}() throws Exception {
${staticImport:importStatic('org.junit.Assert.*')}
}</template></templates>
2 comments:
Hi, this is really useful! I only have a problem: when I fire the "checking" template, I get
context.checking(new Expectations() {
{
}
});
is there a way to tell Eclipse not to start a new line after the first "{" ?
Hi xpmatteo,
Thanks for the feedback.
This is an interesting question but it is more about the code formatter than about the template itself. You have a couple of options that I can see.
1. Disable automatic code formatting for your templates (by unchecking the "Use code formatter" box on the bottom of the templates preference page).
2. Modify Preferences -> Java -> Code Style -> Formatter -> Edit... -> Blank Lines -> Before first declaration: -> 0.
(2.) would be my preference since I automatically format using save actions anyway.
If you find another way, let me know.
I hope that helps!
---Tim---
Post a Comment