Created
October 8, 2011 11:51
-
-
Save eugenp/1272175 to your computer and use it in GitHub Desktop.
Testing the Service Layer - the Foo service unit test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is used in:
http://www.baeldung.com/2011/10/02/testing-the-service-layer/