Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Mock Obects in Spring Integration Tests
package com.jayway.example;
public interface Delegate {
String doSomething();
String doSomethingElse();
}
<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>
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);
}
}
package com.jayway.example;
@Component
public class Facade {
@Autowired
private Delegate delegate;
public String firstMethod() {
return delegate.doSomething();
}
public String secondMethod() {
return delegate.doSomethingElse();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("test-config.xml")
public class FacadeTest {
@Autowired
Facade facade;
@Autowired
Delegate delegateMock;
@Test
public void shouldAutowireDependencies() {
assertNotNull(facade);
assertNotNull(delegateMock);
}
}
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();
}
}
<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>
@Test
public void shouldDelegateToDoSomething() {
String expected = "hello";
when(delegateMock.doSomething()).thenReturn(expected);
String actual = facade.firstMethod();
assertThat(actual, is(expected));
verify(delegateMock).doSomething();
}
@Before
public void setUp() {
reset(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);
}
@matsev

This comment has been minimized.

Copy link
Owner Author

matsev commented Feb 25, 2012

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/ .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.