Skip to content

Instantly share code, notes, and snippets.

@eugenp
Created October 8, 2011 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugenp/1272175 to your computer and use it in GitHub Desktop.
Save eugenp/1272175 to your computer and use it in GitHub Desktop.
Testing the Service Layer - the Foo service unit test
public class FooServiceUnitTest{
FooService instance;
private HibernateTemplate hibernateTemplateMock;
@Before
public final void before(){
this.instance = new FooService();
this.instance.dao = new FooDAO();
this.hibernateTemplateMock = mock( HibernateTemplate.class );
this.instance.dao.setHibernateTemplate( this.hibernateTemplateMock );
}
@Test
public final void whenCreateIsTriggered_thenNoException(){
// When
this.instance.create( new Foo( "testName" ) );
}
@Test( expected = NullPointerException.class )
public final void whenCreateIsTriggeredForNullEntity_thenException(){
// When
this.instance.create( null );
}
@Test
public final void whenCreateIsTriggered_thenEntityIsCreated(){
// When
final Foo entity = new Foo( "testName" );
this.instance.create( entity );
// Then
final ArgumentCaptor< Foo > argument = ArgumentCaptor.forClass( Foo.class );
verify( this.hibernateTemplateMock ).save( argument.capture() );
assertThat( entity, is( argument.getValue() ) );
}
}
@eugenp
Copy link
Author

eugenp commented Feb 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment