Created
December 3, 2011 20:02
-
-
Save matsev/1427987 to your computer and use it in GitHub Desktop.
Using Mock Obects in Spring Integration Tests
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
package com.jayway.example; | |
public interface Delegate { | |
String doSomething(); | |
String doSomethingElse(); | |
} |
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
<beans...> | |
<context:component-scan base-package="com.jayway.example"/> | |
<bean id="delegateMock" class="com.jayway.springmock.EasyMockFactoryBean"> | |
<constructor-arg name="classToBeMocked" value="com.jayway.example.Delegate" /> | |
</bean> | |
</beans> |
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
package com.jayway.example; | |
import org.junit.After; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import static org.easymock.EasyMock.expect; | |
import static org.easymock.EasyMock.replay; | |
import static org.easymock.EasyMock.reset; | |
import static org.easymock.EasyMock.verify; | |
import static org.hamcrest.core.Is.is; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertThat; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration("easymock-test-config.xml") | |
public class EasyMockFacadeTest { | |
@Autowired | |
Facade facade; | |
@Autowired | |
Delegate delegateMock; | |
@Before | |
public void setUp() { | |
reset(delegateMock); | |
} | |
@Test | |
public void shouldAutowireDependencies() { | |
assertNotNull(facade); | |
assertNotNull(delegateMock); | |
} | |
@Test | |
public void shouldDelegateToDoSomething() { | |
String expected = "hello"; | |
expect(delegateMock.doSomething()).andReturn(expected); | |
replay(delegateMock); | |
String actual = facade.firstMethod(); | |
assertThat(actual, is(expected)); | |
verify(delegateMock); | |
} | |
@Test | |
public void shouldDelegateToDoSomethingElse() { | |
String expected = "goodbye"; | |
expect(delegateMock.doSomethingElse()).andReturn(expected); | |
replay(delegateMock); | |
String actual = facade.secondMethod(); | |
assertThat(actual, is(expected)); | |
verify(delegateMock); | |
} | |
} |
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
package com.jayway.example; | |
@Component | |
public class Facade { | |
@Autowired | |
private Delegate delegate; | |
public String firstMethod() { | |
return delegate.doSomething(); | |
} | |
public String secondMethod() { | |
return delegate.doSomethingElse(); | |
} | |
} |
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
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration("test-config.xml") | |
public class FacadeTest { | |
@Autowired | |
Facade facade; | |
@Autowired | |
Delegate delegateMock; | |
@Test | |
public void shouldAutowireDependencies() { | |
assertNotNull(facade); | |
assertNotNull(delegateMock); | |
} | |
} |
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
package com.jayway.example; | |
import org.junit.After; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import static org.hamcrest.core.Is.is; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertThat; | |
import static org.mockito.Mockito.reset; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.Mockito.when; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration("mockito-test-config.xml") | |
public class MockitoFacadeTest { | |
@Autowired | |
Facade facade; | |
@Autowired | |
Delegate delegateMock; | |
@Before | |
public void setUp() { | |
reset(delegateMock); | |
} | |
@Test | |
public void shouldWireDependencies() { | |
assertNotNull(facade); | |
assertNotNull(delegateMock); | |
} | |
@Test | |
public void shouldDelegateToDoSomething() { | |
String expected = "hello"; | |
when(delegateMock.doSomething()).thenReturn(expected); | |
String actual = facade.firstMethod(); | |
assertThat(actual, is(expected)); | |
verify(delegateMock).doSomething(); | |
} | |
@Test | |
public void shouldDelegateToDoSomethingElse() { | |
String expected = "goodbye"; | |
when(delegateMock.doSomethingElse()).thenReturn(expected); | |
String actual = facade.secondMethod(); | |
assertThat(actual, is(expected)); | |
verify(delegateMock).doSomethingElse(); | |
} | |
} |
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
<beans...> | |
<context:component-scan base-package="com.jayway.example"/> | |
<bean id="delegateMock" class="com.jayway.springmock.MockitoFactoryBean"> | |
<constructor-arg name="classToBeMocked" value="com.jayway.example.Delegate" /> | |
</bean> | |
</beans> |
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
@Test | |
public void shouldDelegateToDoSomething() { | |
String expected = "hello"; | |
when(delegateMock.doSomething()).thenReturn(expected); | |
String actual = facade.firstMethod(); | |
assertThat(actual, is(expected)); | |
verify(delegateMock).doSomething(); | |
} |
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
@Before | |
public void setUp() { | |
reset(delegateMock); | |
} |
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
@Test | |
public void shouldDelegateToDoSomethingElse() { | |
String expected = "goodbye"; | |
expect(delegateMock.doSomethingElse()).andReturn(expected); | |
replay(delegateMock); | |
String actual = facade.secondMethod(); | |
assertThat(actual, is(expected)); | |
verify(delegateMock); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code in this gist is used to demonstrate how mock objects can be used when developing Spring integration tests. For details, read the blog post at http://blog.jayway.com/2011/12/12/spring-integration-tests-part-ii-using-mock-objects/ .