Skip to content

Commit

Permalink
Replace EasyMock with Mockito in spring-jms
Browse files Browse the repository at this point in the history
Issue: SPR-10126
  • Loading branch information
philwebb committed Feb 10, 2013
1 parent c4f79bb commit 2642cf2
Show file tree
Hide file tree
Showing 20 changed files with 1,448 additions and 2,803 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ configure(allprojects) { project ->
testCompile("junit:junit:${junitVersion}")
testCompile("org.hamcrest:hamcrest-all:1.3")
testCompile("org.mockito:mockito-core:1.9.5")
if (project.name in ["spring", "spring-jms",
if (project.name in ["spring",
"spring-orm-hibernate4", "spring-oxm", "spring-struts",
"spring-test", "spring-test-mvc", "spring-tx", "spring-web",
"spring-webmvc", "spring-webmvc-portlet", "spring-webmvc-tiles3"]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@

package org.springframework.jms.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import junit.framework.TestCase;
import org.easymock.MockControl;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.ComponentDefinition;
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
Expand All @@ -42,14 +50,15 @@
import org.springframework.jca.endpoint.GenericMessageEndpointManager;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.endpoint.JmsMessageEndpointManager;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.util.ErrorHandler;

/**
* @author Mark Fisher
* @author Juergen Hoeller
* @author Christian Dupuis
*/
public class JmsNamespaceHandlerTests extends TestCase {
public class JmsNamespaceHandlerTests {

private static final String DEFAULT_CONNECTION_FACTORY = "connectionFactory";

Expand All @@ -58,17 +67,18 @@ public class JmsNamespaceHandlerTests extends TestCase {
private ToolingTestApplicationContext context;


@Override
protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
this.context = new ToolingTestApplicationContext("jmsNamespaceHandlerTests.xml", getClass());
}

@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
this.context.close();
}


@Test
public void testBeansCreated() {
Map containers = context.getBeansOfType(DefaultMessageListenerContainer.class);
assertEquals("Context should contain 3 JMS listener containers", 3, containers.size());
Expand All @@ -77,6 +87,7 @@ public void testBeansCreated() {
assertEquals("Context should contain 3 JCA endpoint containers", 3, containers.size());
}

@Test
public void testContainerConfiguration() throws Exception {
Map<String, DefaultMessageListenerContainer> containers = context.getBeansOfType(DefaultMessageListenerContainer.class);
ConnectionFactory defaultConnectionFactory = context.getBean(DEFAULT_CONNECTION_FACTORY, ConnectionFactory.class);
Expand All @@ -102,6 +113,7 @@ else if (container.getConnectionFactory().equals(explicitConnectionFactory)) {
assertEquals("2 containers should have the explicit connectionFactory", 2, explicitConnectionFactoryCount);
}

@Test
public void testListeners() throws Exception {
TestBean testBean1 = context.getBean("testBean1", TestBean.class);
TestBean testBean2 = context.getBean("testBean2", TestBean.class);
Expand All @@ -111,36 +123,28 @@ public void testListeners() throws Exception {
assertNull(testBean2.getName());
assertNull(testBean3.message);

MockControl control1 = MockControl.createControl(TextMessage.class);
TextMessage message1 = (TextMessage) control1.getMock();
control1.expectAndReturn(message1.getText(), "Test1");
control1.replay();
TextMessage message1 = mock(TextMessage.class);
given(message1.getText()).willReturn("Test1");

MessageListener listener1 = getListener("listener1");
listener1.onMessage(message1);
assertEquals("Test1", testBean1.getName());
control1.verify();

MockControl control2 = MockControl.createControl(TextMessage.class);
TextMessage message2 = (TextMessage) control2.getMock();
control2.expectAndReturn(message2.getText(), "Test2");
control2.replay();
TextMessage message2 = mock(TextMessage.class);
given(message2.getText()).willReturn("Test2");

MessageListener listener2 = getListener("listener2");
listener2.onMessage(message2);
assertEquals("Test2", testBean2.getName());
control2.verify();

MockControl control3 = MockControl.createControl(TextMessage.class);
TextMessage message3 = (TextMessage) control3.getMock();
control3.replay();
TextMessage message3 = mock(TextMessage.class);

MessageListener listener3 = getListener(DefaultMessageListenerContainer.class.getName() + "#0");
listener3.onMessage(message3);
assertSame(message3, testBean3.message);
control3.verify();
}

@Test
public void testErrorHandlers() {
ErrorHandler expected = this.context.getBean("testErrorHandler", ErrorHandler.class);
ErrorHandler errorHandler1 = getErrorHandler("listener1");
Expand All @@ -151,6 +155,7 @@ public void testErrorHandlers() {
assertNull(defaultErrorHandler);
}

@Test
public void testPhases() {
int phase1 = getPhase("listener1");
int phase2 = getPhase("listener2");
Expand Down Expand Up @@ -182,6 +187,7 @@ public int getPhase(String containerBeanName) {
return ((Phased) container).getPhase();
}

@Test
public void testComponentRegistration() {
assertTrue("Parser should have registered a component named 'listener1'", context.containsComponentDefinition("listener1"));
assertTrue("Parser should have registered a component named 'listener2'", context.containsComponentDefinition("listener2"));
Expand All @@ -192,6 +198,7 @@ public void testComponentRegistration() {
context.containsComponentDefinition(JmsMessageEndpointManager.class.getName() + "#0"));
}

@Test
public void testSourceExtraction() {
Iterator iterator = context.getRegisteredComponents();
while (iterator.hasNext()) {
Expand Down
Loading

0 comments on commit 2642cf2

Please sign in to comment.