Skip to content

Instantly share code, notes, and snippets.

@Christopher-Barham-AKQA
Created August 27, 2012 09:47
Show Gist options
  • Save Christopher-Barham-AKQA/3487015 to your computer and use it in GitHub Desktop.
Save Christopher-Barham-AKQA/3487015 to your computer and use it in GitHub Desktop.
Eclipse Template Unit Test Import
Eclipse Template Unit Test Import
In Eclipse go Preferences>Java>Edito>Templates>New
• Name: jimp
• context: Java
• Description: Unit test imports
• Pattern:
import static java.lang.String.format;
import static org.mockito.BDDMockito.*;
import static org.junit.Assume.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.mockito.*;
import org.mockito.runners.*;
import org.junit.*;
import org.junit.runner.*;
Then type jimp+CTRL+SPACE to unit test imports inserted.
given, When, Then Template
Tests look something like this in BDD stylee....
import static org.mockito.BDDMockito.*;
Seller seller = mock(Seller.class);
Shop shop = new Shop(seller);
public void shouldBuyBread() throws Exception {
//given
given(seller.askForBread()).willReturn(new Bread());
//when
Goods goods = shop.buyBread();
//then
assertThat(goods, containBread());
}
To minimise keystrokes and encourage the use of this style, you can modify the existing Test template as shown below to automatically generate the BDD style comments & statically import the BDDMockito members. To modify the template, navigate to Window > Preferences > Java > Editor > Templates. Click on Test (the JUnit 4 test template) and click edit. Paste in the follow template:
@${testType:newType(org.junit.Test)}
public void ${testname}() throws Exception {
// given ${cursor}
${staticImport:importStatic('org.junit.Assert.*', 'org.mockito.BDDMockito.*')}
// when
// then
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment