Replace EasyMock with Mockito in test sources

Issue: SPR-10126
This commit is contained in:
Phillip Webb
2012-12-19 14:45:29 -08:00
committed by Chris Beams
parent cbf6991d47
commit d66c733ef4
82 changed files with 4828 additions and 10460 deletions

View File

@@ -16,8 +16,9 @@
package org.springframework.aop.aspectj;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
@@ -55,56 +56,44 @@ public final class AfterAdviceBindingTests {
// we need the real target too, not just the proxy...
testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();
mockCollaborator = createNiceMock(AdviceBindingCollaborator.class);
mockCollaborator = mock(AdviceBindingCollaborator.class);
afterAdviceAspect.setCollaborator(mockCollaborator);
}
@Test
public void testOneIntArg() {
mockCollaborator.oneIntArg(5);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntArg(5);
}
@Test
public void testOneObjectArgBindingProxyWithThis() {
mockCollaborator.oneObjectArg(this.testBeanProxy);
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).oneObjectArg(this.testBeanProxy);
}
@Test
public void testOneObjectArgBindingTarget() {
mockCollaborator.oneObjectArg(this.testBeanTarget);
replay(mockCollaborator);
testBeanProxy.getDoctor();
verify(mockCollaborator);
verify(mockCollaborator).oneObjectArg(this.testBeanTarget);
}
@Test
public void testOneIntAndOneObjectArgs() {
mockCollaborator.oneIntAndOneObject(5,this.testBeanProxy);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntAndOneObject(5,this.testBeanProxy);
}
@Test
public void testNeedsJoinPoint() {
mockCollaborator.needsJoinPoint("getAge");
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).needsJoinPoint("getAge");
}
@Test
public void testNeedsJoinPointStaticPart() {
mockCollaborator.needsJoinPointStaticPart("getAge");
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).needsJoinPointStaticPart("getAge");
}
}

View File

@@ -16,8 +16,10 @@
package org.springframework.aop.aspectj;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import org.junit.Before;
import org.junit.Test;
@@ -58,7 +60,7 @@ public final class AfterReturningAdviceBindingTests {
afterAdviceAspect = (AfterReturningAdviceBindingTestAspect) ctx.getBean("testAspect");
mockCollaborator = createNiceMock(AfterReturningAdviceBindingCollaborator.class);
mockCollaborator = mock(AfterReturningAdviceBindingCollaborator.class);
afterAdviceAspect.setCollaborator(mockCollaborator);
testBeanProxy = (ITestBean) ctx.getBean("testBean");
@@ -71,106 +73,79 @@ public final class AfterReturningAdviceBindingTests {
@Test
public void testOneIntArg() {
mockCollaborator.oneIntArg(5);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntArg(5);
}
@Test
public void testOneObjectArg() {
mockCollaborator.oneObjectArg(this.testBeanProxy);
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).oneObjectArg(this.testBeanProxy);
}
@Test
public void testOneIntAndOneObjectArgs() {
mockCollaborator.oneIntAndOneObject(5,this.testBeanProxy);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntAndOneObject(5,this.testBeanProxy);
}
@Test
public void testNeedsJoinPoint() {
mockCollaborator.needsJoinPoint("getAge");
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).needsJoinPoint("getAge");
}
@Test
public void testNeedsJoinPointStaticPart() {
mockCollaborator.needsJoinPointStaticPart("getAge");
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).needsJoinPointStaticPart("getAge");
}
@Test
public void testReturningString() {
mockCollaborator.oneString("adrian");
replay(mockCollaborator);
testBeanProxy.setName("adrian");
testBeanProxy.getName();
verify(mockCollaborator);
verify(mockCollaborator).oneString("adrian");
}
@Test
public void testReturningObject() {
mockCollaborator.oneObjectArg(this.testBeanTarget);
replay(mockCollaborator);
testBeanProxy.returnsThis();
verify(mockCollaborator);
verify(mockCollaborator).oneObjectArg(this.testBeanTarget);
}
@Test
public void testReturningBean() {
mockCollaborator.oneTestBeanArg(this.testBeanTarget);
replay(mockCollaborator);
testBeanProxy.returnsThis();
verify(mockCollaborator);
verify(mockCollaborator).oneTestBeanArg(this.testBeanTarget);
}
@Test
public void testReturningBeanArray() {
this.testBeanTarget.setSpouse(new TestBean());
ITestBean[] spouses = this.testBeanTarget.getSpouses();
mockCollaborator.testBeanArrayArg(spouses);
replay(mockCollaborator);
testBeanProxy.getSpouses();
verify(mockCollaborator);
verify(mockCollaborator).testBeanArrayArg(spouses);
}
@Test
public void testNoInvokeWhenReturningParameterTypeDoesNotMatch() {
// we need a strict mock for this...
mockCollaborator = createMock(AfterReturningAdviceBindingCollaborator.class);
afterAdviceAspect.setCollaborator(mockCollaborator);
replay(mockCollaborator);
testBeanProxy.setSpouse(this.testBeanProxy);
testBeanProxy.getSpouse();
verify(mockCollaborator);
verifyZeroInteractions(mockCollaborator);
}
@Test
public void testReturningByType() {
mockCollaborator.objectMatchNoArgs();
replay(mockCollaborator);
testBeanProxy.returnsThis();
verify(mockCollaborator);
verify(mockCollaborator).objectMatchNoArgs();
}
@Test
public void testReturningPrimitive() {
mockCollaborator.oneInt(20);
replay(mockCollaborator);
testBeanProxy.setAge(20);
testBeanProxy.haveBirthday();
verify(mockCollaborator);
verify(mockCollaborator).oneInt(20);
}
}

View File

@@ -16,9 +16,9 @@
package org.springframework.aop.aspectj;
import static org.easymock.EasyMock.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.aspectj.AfterThrowingAdviceBindingTestAspect.AfterThrowingAdviceBindingCollaborator;
@@ -47,65 +47,49 @@ public final class AfterThrowingAdviceBindingTests {
testBean = (ITestBean) ctx.getBean("testBean");
afterThrowingAdviceAspect = (AfterThrowingAdviceBindingTestAspect) ctx.getBean("testAspect");
mockCollaborator = createNiceMock(AfterThrowingAdviceBindingCollaborator.class);
mockCollaborator = mock(AfterThrowingAdviceBindingCollaborator.class);
afterThrowingAdviceAspect.setCollaborator(mockCollaborator);
}
@After
public void tearDown() {
verify(mockCollaborator);
}
@Test(expected=Throwable.class)
public void testSimpleAfterThrowing() throws Throwable {
mockCollaborator.noArgs();
replay(mockCollaborator);
this.testBean.exceptional(new Throwable());
verify(mockCollaborator).noArgs();
}
@Test(expected=Throwable.class)
public void testAfterThrowingWithBinding() throws Throwable {
Throwable t = new Throwable();
mockCollaborator.oneThrowable(t);
replay(mockCollaborator);
this.testBean.exceptional(t);
verify(mockCollaborator).oneThrowable(t);
}
@Test(expected=Throwable.class)
public void testAfterThrowingWithNamedTypeRestriction() throws Throwable {
Throwable t = new Throwable();
// need a strict mock for this test...
mockCollaborator = createMock(AfterThrowingAdviceBindingCollaborator.class);
afterThrowingAdviceAspect.setCollaborator(mockCollaborator);
mockCollaborator.noArgs();
mockCollaborator.oneThrowable(t);
mockCollaborator.noArgsOnThrowableMatch();
replay(mockCollaborator);
this.testBean.exceptional(t);
verify(mockCollaborator).noArgs();
verify(mockCollaborator).oneThrowable(t);
verify(mockCollaborator).noArgsOnThrowableMatch();
}
@Test(expected=Throwable.class)
public void testAfterThrowingWithRuntimeExceptionBinding() throws Throwable {
RuntimeException ex = new RuntimeException();
mockCollaborator.oneRuntimeException(ex);
replay(mockCollaborator);
this.testBean.exceptional(ex);
verify(mockCollaborator).oneRuntimeException(ex);
}
@Test(expected=Throwable.class)
public void testAfterThrowingWithTypeSpecified() throws Throwable {
mockCollaborator.noArgsOnThrowableMatch();
replay(mockCollaborator);
this.testBean.exceptional(new Throwable());
verify(mockCollaborator).noArgsOnThrowableMatch();
}
@Test(expected=Throwable.class)
public void testAfterThrowingWithRuntimeTypeSpecified() throws Throwable {
mockCollaborator.noArgsOnRuntimeExceptionMatch();
replay(mockCollaborator);
this.testBean.exceptional(new RuntimeException());
verify(mockCollaborator);
verify(mockCollaborator).noArgsOnRuntimeExceptionMatch();
}
}

View File

@@ -16,8 +16,9 @@
package org.springframework.aop.aspectj;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.aspectj.lang.ProceedingJoinPoint;
import org.junit.Before;
@@ -60,40 +61,32 @@ public class AroundAdviceBindingTests {
this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();
mockCollaborator = createNiceMock(AroundAdviceBindingCollaborator.class);
mockCollaborator = mock(AroundAdviceBindingCollaborator.class);
aroundAdviceAspect.setCollaborator(mockCollaborator);
}
@Test
public void testOneIntArg() {
mockCollaborator.oneIntArg(5);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntArg(5);
}
@Test
public void testOneObjectArgBoundToTarget() {
mockCollaborator.oneObjectArg(this.testBeanTarget);
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).oneObjectArg(this.testBeanTarget);;
}
@Test
public void testOneIntAndOneObjectArgs() {
mockCollaborator.oneIntAndOneObject(5, this.testBeanProxy);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntAndOneObject(5, this.testBeanProxy);;
}
@Test
public void testJustJoinPoint() {
mockCollaborator.justJoinPoint("getAge");
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).justJoinPoint("getAge");;
}
}

View File

@@ -16,8 +16,9 @@
package org.springframework.aop.aspectj;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
@@ -60,49 +61,39 @@ public final class BeforeAdviceBindingTests {
AdviceBindingTestAspect beforeAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect");
mockCollaborator = createNiceMock(AdviceBindingCollaborator.class);
mockCollaborator = mock(AdviceBindingCollaborator.class);
beforeAdviceAspect.setCollaborator(mockCollaborator);
}
@Test
public void testOneIntArg() {
mockCollaborator.oneIntArg(5);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntArg(5);
}
@Test
public void testOneObjectArgBoundToProxyUsingThis() {
mockCollaborator.oneObjectArg(this.testBeanProxy);
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).oneObjectArg(this.testBeanProxy);
}
@Test
public void testOneIntAndOneObjectArgs() {
mockCollaborator.oneIntAndOneObject(5,this.testBeanTarget);
replay(mockCollaborator);
testBeanProxy.setAge(5);
verify(mockCollaborator);
verify(mockCollaborator).oneIntAndOneObject(5,this.testBeanTarget);
}
@Test
public void testNeedsJoinPoint() {
mockCollaborator.needsJoinPoint("getAge");
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).needsJoinPoint("getAge");
}
@Test
public void testNeedsJoinPointStaticPart() {
mockCollaborator.needsJoinPointStaticPart("getAge");
replay(mockCollaborator);
testBeanProxy.getAge();
verify(mockCollaborator);
verify(mockCollaborator).needsJoinPointStaticPart("getAge");
}

View File

@@ -16,14 +16,17 @@
package org.springframework.aop.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.lang.reflect.Method;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
/**
@@ -39,40 +42,27 @@ public final class MethodLocatingFactoryBeanTests {
@Before
public void setUp() {
factory = new MethodLocatingFactoryBean();
// methods must set up expectations and call replay() manually for this mock
beanFactory = createMock(BeanFactory.class);
}
@After
public void tearDown() {
verify(beanFactory);
beanFactory = mock(BeanFactory.class);
}
@Test
public void testIsSingleton() {
replay(beanFactory);
assertTrue(factory.isSingleton());
}
@Test
public void testGetObjectType() {
replay(beanFactory);
assertEquals(Method.class, factory.getObjectType());
}
@Test(expected=IllegalArgumentException.class)
public void testWithNullTargetBeanName() {
replay(beanFactory);
factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory);
}
@Test(expected=IllegalArgumentException.class)
public void testWithEmptyTargetBeanName() {
replay(beanFactory);
factory.setTargetBeanName("");
factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory);
@@ -80,16 +70,12 @@ public final class MethodLocatingFactoryBeanTests {
@Test(expected=IllegalArgumentException.class)
public void testWithNullTargetMethodName() {
replay(beanFactory);
factory.setTargetBeanName(BEAN_NAME);
factory.setBeanFactory(beanFactory);
}
@Test(expected=IllegalArgumentException.class)
public void testWithEmptyTargetMethodName() {
replay(beanFactory);
factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName("");
factory.setBeanFactory(beanFactory);
@@ -97,20 +83,16 @@ public final class MethodLocatingFactoryBeanTests {
@Test(expected=IllegalArgumentException.class)
public void testWhenTargetBeanClassCannotBeResolved() {
expect(beanFactory.getType(BEAN_NAME)).andReturn(null);
replay(beanFactory);
factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory);
verify(beanFactory).getType(BEAN_NAME);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("unchecked")
public void testSunnyDayPath() throws Exception {
expect((Class) beanFactory.getType(BEAN_NAME)).andReturn(String.class);
replay(beanFactory);
given(beanFactory.getType(BEAN_NAME)).willReturn((Class)String.class);
factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory);
@@ -122,11 +104,9 @@ public final class MethodLocatingFactoryBeanTests {
}
@Test(expected=IllegalArgumentException.class)
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("unchecked")
public void testWhereMethodCannotBeResolved() {
expect((Class) beanFactory.getType(BEAN_NAME)).andReturn(String.class);
replay(beanFactory);
given(beanFactory.getType(BEAN_NAME)).willReturn((Class)String.class);
factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName("loadOfOld()");
factory.setBeanFactory(beanFactory);

View File

@@ -16,11 +16,9 @@
package org.springframework.aop.framework;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import java.io.Serializable;
@@ -80,18 +78,16 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
public void testInterceptorIsInvokedWithNoTarget() throws Throwable {
// Test return value
int age = 25;
MethodInterceptor mi = createMock(MethodInterceptor.class);
MethodInterceptor mi = mock(MethodInterceptor.class);
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
pc.addAdvice(mi);
AopProxy aop = createAopProxy(pc);
expect(mi.invoke(null)).andReturn(age);
replay(mi);
given(mi.invoke(null)).willReturn(age);
ITestBean tb = (ITestBean) aop.getProxy();
assertTrue("correct return value", tb.getAge() == age);
verify(mi);
}
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable {

View File

@@ -16,8 +16,9 @@
package org.springframework.context.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
@@ -32,10 +33,7 @@ public class ContextBeanFactoryReferenceTests {
@Test
public void testAllOperations() {
ConfigurableApplicationContext ctx = createMock(ConfigurableApplicationContext.class);
ctx.close();
replay(ctx);
ConfigurableApplicationContext ctx = mock(ConfigurableApplicationContext.class);
ContextBeanFactoryReference bfr = new ContextBeanFactoryReference(ctx);
@@ -49,6 +47,6 @@ public class ContextBeanFactoryReferenceTests {
// expected
}
verify(ctx);
verify(ctx).close();
}
}

View File

@@ -16,10 +16,10 @@
package org.springframework.context.annotation;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import java.lang.instrument.ClassFileTransformer;
@@ -40,7 +40,7 @@ public class EnableLoadTimeWeavingTests {
public void control() {
GenericXmlApplicationContext ctx =
new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
ctx.getBean("loadTimeWeaver");
ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
}
@Test
@@ -48,7 +48,8 @@ public class EnableLoadTimeWeavingTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EnableLTWConfig_withAjWeavingDisabled.class);
ctx.refresh();
ctx.getBean("loadTimeWeaver");
LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
verifyZeroInteractions(loadTimeWeaver);
}
@Test
@@ -56,7 +57,10 @@ public class EnableLoadTimeWeavingTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EnableLTWConfig_withAjWeavingAutodetect.class);
ctx.refresh();
ctx.getBean("loadTimeWeaver");
LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
// no expectations -> a class file transformer should NOT be added
// because no META-INF/aop.xml is present on the classpath
verifyZeroInteractions(loadTimeWeaver);
}
@Test
@@ -64,7 +68,8 @@ public class EnableLoadTimeWeavingTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EnableLTWConfig_withAjWeavingEnabled.class);
ctx.refresh();
ctx.getBean("loadTimeWeaver");
LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
verify(loadTimeWeaver).addTransformer(isA(ClassFileTransformer.class));
}
@Configuration
@@ -72,10 +77,7 @@ public class EnableLoadTimeWeavingTests {
static class EnableLTWConfig_withAjWeavingDisabled implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
LoadTimeWeaver mockLTW = createMock(LoadTimeWeaver.class);
// no expectations -> a class file transformer should NOT be added
replay(mockLTW);
return mockLTW;
return mock(LoadTimeWeaver.class);
}
}
@@ -84,11 +86,7 @@ public class EnableLoadTimeWeavingTests {
static class EnableLTWConfig_withAjWeavingAutodetect implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
LoadTimeWeaver mockLTW = createMock(LoadTimeWeaver.class);
// no expectations -> a class file transformer should NOT be added
// because no META-INF/aop.xml is present on the classpath
replay(mockLTW);
return mockLTW;
return mock(LoadTimeWeaver.class);
}
}
@@ -97,11 +95,7 @@ public class EnableLoadTimeWeavingTests {
static class EnableLTWConfig_withAjWeavingEnabled implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
LoadTimeWeaver mockLTW = createMock(LoadTimeWeaver.class);
mockLTW.addTransformer(isA(ClassFileTransformer.class));
expectLastCall();
replay(mockLTW);
return mockLTW;
return mock(LoadTimeWeaver.class);
}
}
}

View File

@@ -20,9 +20,12 @@ import java.util.HashSet;
import java.util.Set;
import org.aopalliance.intercept.MethodInvocation;
import org.easymock.EasyMock;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
@@ -50,14 +53,12 @@ public class ApplicationContextEventTests {
@SuppressWarnings("unchecked")
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
listener.onApplicationEvent(evt);
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(listener);
replay(listener);
smc.multicastEvent(evt);
verify(listener);
verify(listener).onApplicationEvent(evt);
}
@Test
@@ -91,20 +92,18 @@ public class ApplicationContextEventTests {
@Test
public void testEventPublicationInterceptor() throws Throwable {
MethodInvocation invocation = EasyMock.createMock(MethodInvocation.class);
ApplicationContext ctx = EasyMock.createMock(ApplicationContext.class);
MethodInvocation invocation = mock(MethodInvocation.class);
ApplicationContext ctx = mock(ApplicationContext.class);
EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
interceptor.setApplicationEventClass(MyEvent.class);
interceptor.setApplicationEventPublisher(ctx);
interceptor.afterPropertiesSet();
expect(invocation.proceed()).andReturn(new Object());
expect(invocation.getThis()).andReturn(new Object());
ctx.publishEvent(isA(MyEvent.class));
replay(invocation, ctx);
given(invocation.proceed()).willReturn(new Object());
given(invocation.getThis()).willReturn(new Object());
interceptor.invoke(invocation);
verify(invocation, ctx);
verify(ctx).publishEvent(isA(MyEvent.class));
}
@Test

View File

@@ -16,8 +16,8 @@
package org.springframework.context.event;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import org.junit.Before;
import org.junit.Test;
@@ -44,13 +44,7 @@ public class EventPublicationInterceptorTests {
@Before
public void setUp() {
publisher = createMock(ApplicationEventPublisher.class);
replay(publisher);
}
@After
public void tearDown() {
verify(publisher);
publisher = mock(ApplicationEventPublisher.class);
}
@Test(expected=IllegalArgumentException.class)

View File

@@ -16,8 +16,10 @@
package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
@@ -41,15 +43,14 @@ public class LocalSlsbInvokerInterceptorTests {
*/
@Test
public void testPerformsLookup() throws Exception {
LocalInterfaceWithBusinessMethods ejb = createMock(LocalInterfaceWithBusinessMethods.class);
replay(ejb);
LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class);
String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb);
configuredInterceptor(mockContext, jndiName);
verify(mockContext);
verify(mockContext).close();
}
@Test
@@ -81,10 +82,8 @@ public class LocalSlsbInvokerInterceptorTests {
@Test
public void testInvokesMethodOnEjbInstance() throws Exception {
Object retVal = new Object();
LocalInterfaceWithBusinessMethods ejb = createMock(LocalInterfaceWithBusinessMethods.class);
expect(ejb.targetMethod()).andReturn(retVal);
ejb.remove();
replay(ejb);
LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class);
given(ejb.targetMethod()).willReturn(retVal);
String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb);
@@ -97,17 +96,15 @@ public class LocalSlsbInvokerInterceptorTests {
assertTrue(target.targetMethod() == retVal);
verify(mockContext);
verify(ejb);
verify(mockContext).close();
verify(ejb).remove();
}
@Test
public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception {
Object retVal = new Object();
LocalInterface ejb = createMock(LocalInterface.class);
expect(ejb.targetMethod()).andReturn(retVal);
ejb.remove();
replay(ejb);
LocalInterface ejb = mock(LocalInterface.class);
given(ejb.targetMethod()).willReturn(retVal);
String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb);
@@ -120,14 +117,13 @@ public class LocalSlsbInvokerInterceptorTests {
assertTrue(target.targetMethod() == retVal);
verify(mockContext);
verify(ejb);
verify(mockContext).close();
verify(ejb).remove();
}
private void testException(Exception expected) throws Exception {
LocalInterfaceWithBusinessMethods ejb = createMock(LocalInterfaceWithBusinessMethods.class);
expect(ejb.targetMethod()).andThrow(expected);
replay(ejb);
LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class);
given(ejb.targetMethod()).willThrow(expected);
String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb);
@@ -146,8 +142,7 @@ public class LocalSlsbInvokerInterceptorTests {
assertTrue(thrown == expected);
}
verify(mockContext);
verify(ejb);
verify(mockContext).close();
}
@Test
@@ -157,16 +152,10 @@ public class LocalSlsbInvokerInterceptorTests {
protected Context mockContext(final String jndiName, final Object ejbInstance)
throws Exception {
final SlsbHome mockHome = createMock(SlsbHome.class);
expect(mockHome.create()).andReturn((LocalInterface)ejbInstance);
replay(mockHome);
final Context mockCtx = createMock(Context.class);
expect(mockCtx.lookup("java:comp/env/" + jndiName)).andReturn(mockHome);
mockCtx.close();
replay(mockCtx);
SlsbHome mockHome = mock(SlsbHome.class);
given(mockHome.create()).willReturn((LocalInterface)ejbInstance);
Context mockCtx = mock(Context.class);
given(mockCtx.lookup("java:comp/env/" + jndiName)).willReturn(mockHome);
return mockCtx;
}

View File

@@ -16,8 +16,11 @@
package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import java.lang.reflect.Proxy;
@@ -42,14 +45,11 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
final int value = 11;
final String jndiName = "foo";
MyEjb myEjb = createMock(MyEjb.class);
expect(myEjb.getValue()).andReturn(value);
myEjb.remove();
replay(myEjb);
MyEjb myEjb = mock(MyEjb.class);
given(myEjb.getValue()).willReturn(value);
final MyHome home = createMock(MyHome.class);
expect(home.create()).andReturn(myEjb);
replay(home);
final MyHome home = mock(MyHome.class);
given(home.create()).willReturn(myEjb);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -72,8 +72,7 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertTrue(mbm.getValue() == value);
verify(myEjb);
verify(home);
verify(myEjb).remove();
}
@Test
@@ -81,9 +80,8 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
final int value = 11;
final String jndiName = "foo";
final MyEjb myEjb = createMock(MyEjb.class);
expect(myEjb.getValue()).andReturn(value);
replay(myEjb);
final MyEjb myEjb = mock(MyEjb.class);
given(myEjb.getValue()).willReturn(value);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -106,7 +104,6 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertTrue(mbm.getValue() == value);
verify(myEjb);
}
@Test
@@ -114,9 +111,8 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
final String jndiName = "foo";
final CreateException cex = new CreateException();
final MyHome home = createMock(MyHome.class);
expect(home.create()).andThrow(cex);
replay(home);
final MyHome home = mock(MyHome.class);
given(home.create()).willThrow(cex);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -147,8 +143,6 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
catch (EjbAccessException ex) {
assertSame(cex, ex.getCause());
}
verify(home);
}
@Test
@@ -157,8 +151,7 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
// Could actually try to figure out interface from create?
final String jndiName = "foo";
final MyHome home = createMock(MyHome.class);
replay(home);
final MyHome home = mock(MyHome.class);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -188,7 +181,7 @@ public class LocalStatelessSessionProxyFactoryBeanTests {
}
// Expect no methods on home
verify(home);
verifyZeroInteractions(home);
}

View File

@@ -16,8 +16,12 @@
package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.rmi.ConnectException;
import java.rmi.RemoteException;
@@ -41,20 +45,12 @@ import org.springframework.remoting.RemoteAccessException;
public class SimpleRemoteSlsbInvokerInterceptorTests {
private Context mockContext(
String jndiName, RemoteInterface ejbInstance, int createCount, int lookupCount, int closeCount)
String jndiName, RemoteInterface ejbInstance)
throws Exception {
final SlsbHome mockHome = createMock(SlsbHome.class);
expect(mockHome.create()).andReturn(ejbInstance).times(createCount);
replay(mockHome);
final Context mockCtx = createMock(Context.class);
expect(mockCtx.lookup("java:comp/env/" + jndiName)).andReturn(mockHome).times(lookupCount);
mockCtx.close();
expectLastCall().times(closeCount);
replay(mockCtx);
SlsbHome mockHome = mock(SlsbHome.class);
given(mockHome.create()).willReturn(ejbInstance);
Context mockCtx = mock(Context.class);
given(mockCtx.lookup("java:comp/env/" + jndiName)).willReturn(mockHome);
return mockCtx;
}
@@ -88,33 +84,32 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
@Test
public void testPerformsLookup() throws Exception {
RemoteInterface ejb = createMock(RemoteInterface.class);
replay(ejb);
RemoteInterface ejb = mock(RemoteInterface.class);
String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
configuredProxy(si, RemoteInterface.class);
verify(mockContext);
verify(mockContext).close();
}
@Test
public void testPerformsLookupWithAccessContext() throws Exception {
RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andReturn(null);
replay(ejb);
RemoteInterface ejb = mock(RemoteInterface.class);
String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 1, 1, 2);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setExposeAccessContext(true);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertNull(target.targetMethod());
verify(mockContext);
verify(mockContext, times(2)).close();
verify(ejb).targetMethod();
}
@Test
@@ -165,10 +160,8 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
private void doTestInvokesMethodOnEjbInstance(boolean lookupHomeOnStartup, boolean cacheHome) throws Exception {
Object retVal = new Object();
final RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andReturn(retVal).times(2);
ejb.remove();
replay(ejb);
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willReturn(retVal);
int lookupCount = 1;
if (!cacheHome) {
@@ -179,7 +172,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
}
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 2, lookupCount, lookupCount);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setLookupHomeOnStartup(lookupHomeOnStartup);
@@ -189,19 +182,18 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
assertTrue(target.targetMethod() == retVal);
assertTrue(target.targetMethod() == retVal);
verify(mockContext, ejb);
verify(mockContext, times(lookupCount)).close();
verify(ejb, times(2)).remove();
}
@Test
public void testInvokesMethodOnEjbInstanceWithHomeInterface() throws Exception {
Object retVal = new Object();
final RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andReturn(retVal);
ejb.remove();
replay(ejb);
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willReturn(retVal);
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setHomeInterface(SlsbHome.class);
@@ -209,18 +201,18 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertTrue(target.targetMethod() == retVal);
verify(mockContext, ejb);
verify(mockContext).close();
verify(ejb).remove();
}
@Test
public void testInvokesMethodOnEjbInstanceWithRemoteException() throws Exception {
final RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andThrow(new RemoteException());
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willThrow(new RemoteException());
ejb.remove();
replay(ejb);
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
@@ -233,7 +225,8 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
// expected
}
verify(mockContext, ejb);
verify(mockContext).close();
verify(ejb, times(2)).remove();
}
@Test
@@ -259,11 +252,8 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
private void doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(
boolean lookupHomeOnStartup, boolean cacheHome) throws Exception {
final RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andThrow(new ConnectException("")).times(2);
ejb.remove();
expectLastCall().times(2);
replay(ejb);
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willThrow(new ConnectException(""));
int lookupCount = 2;
if (!cacheHome) {
@@ -274,7 +264,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
}
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 2, lookupCount, lookupCount);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setRefreshHomeOnConnectFailure(true);
@@ -290,37 +280,35 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
// expected
}
verify(mockContext, ejb);
verify(mockContext, times(lookupCount)).close();
verify(ejb, times(2)).remove();
}
@Test
public void testInvokesMethodOnEjbInstanceWithBusinessInterface() throws Exception {
Object retVal = new Object();
final RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andReturn(retVal);
ejb.remove();
replay(ejb);
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willReturn(retVal);
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class);
assertTrue(target.targetMethod() == retVal);
verify(mockContext, ejb);
verify(mockContext).close();
verify(ejb).remove();
}
@Test
public void testInvokesMethodOnEjbInstanceWithBusinessInterfaceWithRemoteException() throws Exception {
final RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andThrow(new RemoteException());
ejb.remove();
replay(ejb);
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willThrow(new RemoteException());
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
@@ -333,7 +321,8 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
// expected
}
verify(mockContext, ejb);
verify(mockContext).close();
verify(ejb).remove();
}
@Test
@@ -347,13 +336,11 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
}
private void doTestException(Exception expected) throws Exception {
final RemoteInterface ejb = createMock(RemoteInterface.class);
expect(ejb.targetMethod()).andThrow(expected);
ejb.remove();
replay(ejb);
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willThrow(expected);
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
@@ -366,7 +353,8 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
assertTrue(thrown == expected);
}
verify(mockContext, ejb);
verify(mockContext).close();
verify(ejb).remove();
}

View File

@@ -16,8 +16,11 @@
package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import java.lang.reflect.Proxy;
import java.rmi.RemoteException;
@@ -56,14 +59,11 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
final int value = 11;
final String jndiName = "foo";
MyEjb myEjb = createMock(MyEjb.class);
expect(myEjb.getValue()).andReturn(value);
myEjb.remove();
replay(myEjb);
MyEjb myEjb = mock(MyEjb.class);
given(myEjb.getValue()).willReturn(value);
final MyHome home = createMock(MyHome.class);
expect(home.create()).andReturn(myEjb);
replay(home);
final MyHome home = mock(MyHome.class);
given(home.create()).willReturn(myEjb);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -86,8 +86,7 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertEquals("Returns expected value", value, mbm.getValue());
verify(myEjb);
verify(home);
verify(myEjb).remove();
}
@Test
@@ -95,9 +94,8 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
final int value = 11;
final String jndiName = "foo";
final MyEjb myEjb = createMock(MyEjb.class);
expect(myEjb.getValue()).andReturn(value);
replay(myEjb);
final MyEjb myEjb = mock(MyEjb.class);
given(myEjb.getValue()).willReturn(value);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -120,7 +118,6 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertEquals("Returns expected value", value, mbm.getValue());
verify(myEjb);
}
@Override
@@ -129,16 +126,13 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
final RemoteException rex = new RemoteException();
final String jndiName = "foo";
MyEjb myEjb = createMock(MyEjb.class);
expect(myEjb.getValue()).andThrow(rex);
MyEjb myEjb = mock(MyEjb.class);
given(myEjb.getValue()).willThrow(rex);
// TODO might want to control this behaviour...
// Do we really want to call remove after a remote exception?
myEjb.remove();
replay(myEjb);
final MyHome home = createMock(MyHome.class);
expect(home.create()).andReturn(myEjb);
replay(home);
final MyHome home = mock(MyHome.class);
given(home.create()).willReturn(myEjb);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -167,8 +161,7 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
catch (RemoteException ex) {
assertSame("Threw expected RemoteException", rex, ex);
}
verify(myEjb);
verify(home);
verify(myEjb).remove();
}
@Test
@@ -176,9 +169,8 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
final String jndiName = "foo";
final CreateException cex = new CreateException();
final MyHome home = createMock(MyHome.class);
expect(home.create()).andThrow(cex);
replay(home);
final MyHome home = mock(MyHome.class);
given(home.create()).willThrow(cex);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -209,8 +201,6 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
catch (RemoteException ex) {
// expected
}
verify(home);
}
@Test
@@ -218,9 +208,8 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
final String jndiName = "foo";
final CreateException cex = new CreateException();
final MyHome home = createMock(MyHome.class);
expect(home.create()).andThrow(cex);
replay(home);
final MyHome home = mock(MyHome.class);
given(home.create()).willThrow(cex);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -251,8 +240,6 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
catch (RemoteAccessException ex) {
assertTrue(ex.getCause() == cex);
}
verify(home);
}
@Test
@@ -261,8 +248,7 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
// Could actually try to figure out interface from create?
final String jndiName = "foo";
final MyHome home = createMock(MyHome.class);
replay(home);
final MyHome home = mock(MyHome.class);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -292,7 +278,7 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
}
// Expect no methods on home
verify(home);
verifyZeroInteractions(home);
}

View File

@@ -16,154 +16,136 @@
package org.springframework.instrument.classloading.glassfish;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import java.lang.instrument.ClassFileTransformer;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.SecureClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.persistence.spi.ClassTransformer;
import org.easymock.ArgumentsMatcher;
import org.easymock.MockControl;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.instrument.classloading.LoadTimeWeaver;
// converting away from old-style EasyMock APIs was problematic with this class
// glassfish dependencies no longer on classpath
@SuppressWarnings("deprecation")
@Ignore
public class GlassFishLoadTimeWeaverTests {
private MockControl loaderCtrl;
private GlassFishClassLoaderAdapter loader;
private LoadTimeWeaver ltw;
private class DummyInstrumentableClassLoader extends SecureClassLoader {
String INSTR_CL_NAME = GlassFishClassLoaderAdapter.INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V2;
public DummyInstrumentableClassLoader() {
super();
}
public DummyInstrumentableClassLoader(ClassLoader parent) {
super(parent);
}
private List<ClassTransformer> v2Transformers = new ArrayList<ClassTransformer>();
private List<ClassFileTransformer> v3Transformers = new ArrayList<ClassFileTransformer>();
public void addTransformer(ClassTransformer transformer) {
v2Transformers.add(transformer);
}
public void addTransformer(ClassFileTransformer transformer) {
v3Transformers.add(transformer);
}
public ClassLoader copy() {
return new DummyInstrumentableClassLoader();
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (INSTR_CL_NAME.equals(name)) {
return this.getClass();
}
return getClass().getClassLoader().loadClass(name);
}
}
@Before
public void setUp() throws Exception {
ltw = new GlassFishLoadTimeWeaver(new DummyInstrumentableClassLoader());
}
@After
public void tearDown() throws Exception {
loaderCtrl.verify();
ltw = null;
}
@Test
public void testGlassFishLoadTimeWeaver() {
try {
ltw = new GlassFishLoadTimeWeaver();
fail("expected exception");
} catch (IllegalArgumentException ex) {
// expected
}
}
@Test
public void testGlassFishLoadTimeWeaverClassLoader() {
try {
ltw = new GlassFishLoadTimeWeaver(null);
fail("expected exception");
} catch (RuntimeException e) {
// expected
}
ClassLoader cl1 = new URLClassLoader(new URL[0]);
ClassLoader cl2 = new URLClassLoader(new URL[0], cl1);
ClassLoader cl3 = new DummyInstrumentableClassLoader(cl2);
ClassLoader cl4 = new URLClassLoader(new URL[0], cl3);
ltw = new GlassFishLoadTimeWeaver(cl4);
assertSame(cl3, ltw.getInstrumentableClassLoader());
cl1 = new URLClassLoader(new URL[0]);
cl2 = new URLClassLoader(new URL[0], cl1);
cl3 = new DummyInstrumentableClassLoader(cl2);
cl4 = new DummyInstrumentableClassLoader(cl3);
ltw = new GlassFishLoadTimeWeaver(cl4);
assertSame(cl4, ltw.getInstrumentableClassLoader());
}
@Test
public void testAddTransformer() {
ClassFileTransformer transformer = MockControl.createNiceControl(ClassFileTransformer.class).getMock();
loaderCtrl.reset();
loader.addTransformer(transformer);
loaderCtrl.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] arg0, Object[] arg1) {
for (int i = 0; i < arg0.length; i++) {
if (arg0 != null && arg0.getClass() != arg1.getClass())
return false;
}
return true;
}
public String toString(Object[] arg0) {
return Arrays.toString(arg0);
}
});
loaderCtrl.replay();
ltw.addTransformer(transformer);
}
@Test
public void testGetThrowawayClassLoader() {
loaderCtrl.reset();
ClassLoader cl = new URLClassLoader(new URL[0]);
loaderCtrl.expectAndReturn(loader.getClassLoader(), cl);
loaderCtrl.replay();
assertSame(ltw.getThrowawayClassLoader(), cl);
}
}
// private MockControl loaderCtrl;
// private GlassFishClassLoaderAdapter loader;
// private LoadTimeWeaver ltw;
//
// private class DummyInstrumentableClassLoader extends SecureClassLoader {
//
// String INSTR_CL_NAME = GlassFishClassLoaderAdapter.INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V2;
//
// public DummyInstrumentableClassLoader() {
// super();
// }
//
// public DummyInstrumentableClassLoader(ClassLoader parent) {
// super(parent);
// }
//
// private List<ClassTransformer> v2Transformers = new ArrayList<ClassTransformer>();
// private List<ClassFileTransformer> v3Transformers = new ArrayList<ClassFileTransformer>();
//
// public void addTransformer(ClassTransformer transformer) {
// v2Transformers.add(transformer);
// }
//
// public void addTransformer(ClassFileTransformer transformer) {
// v3Transformers.add(transformer);
// }
//
// public ClassLoader copy() {
// return new DummyInstrumentableClassLoader();
// }
//
// @Override
// public Class<?> loadClass(String name) throws ClassNotFoundException {
// if (INSTR_CL_NAME.equals(name)) {
// return this.getClass();
// }
//
// return getClass().getClassLoader().loadClass(name);
// }
// }
//
// @Before
// public void setUp() throws Exception {
// ltw = new GlassFishLoadTimeWeaver(new DummyInstrumentableClassLoader());
// }
//
// @After
// public void tearDown() throws Exception {
// loaderCtrl.verify();
// ltw = null;
// }
//
// @Test
// public void testGlassFishLoadTimeWeaver() {
// try {
// ltw = new GlassFishLoadTimeWeaver();
// fail("expected exception");
// } catch (IllegalArgumentException ex) {
// // expected
// }
//
// }
//
// @Test
// public void testGlassFishLoadTimeWeaverClassLoader() {
// try {
// ltw = new GlassFishLoadTimeWeaver(null);
// fail("expected exception");
// } catch (RuntimeException e) {
// // expected
// }
//
// ClassLoader cl1 = new URLClassLoader(new URL[0]);
// ClassLoader cl2 = new URLClassLoader(new URL[0], cl1);
// ClassLoader cl3 = new DummyInstrumentableClassLoader(cl2);
// ClassLoader cl4 = new URLClassLoader(new URL[0], cl3);
//
// ltw = new GlassFishLoadTimeWeaver(cl4);
// assertSame(cl3, ltw.getInstrumentableClassLoader());
//
// cl1 = new URLClassLoader(new URL[0]);
// cl2 = new URLClassLoader(new URL[0], cl1);
// cl3 = new DummyInstrumentableClassLoader(cl2);
// cl4 = new DummyInstrumentableClassLoader(cl3);
//
// ltw = new GlassFishLoadTimeWeaver(cl4);
// assertSame(cl4, ltw.getInstrumentableClassLoader());
// }
//
// @Test
// public void testAddTransformer() {
// ClassFileTransformer transformer = MockControl.createNiceControl(ClassFileTransformer.class).getMock();
// loaderCtrl.reset();
// loader.addTransformer(transformer);
// loaderCtrl.setMatcher(new ArgumentsMatcher() {
//
// public boolean matches(Object[] arg0, Object[] arg1) {
// for (int i = 0; i < arg0.length; i++) {
// if (arg0 != null && arg0.getClass() != arg1.getClass())
// return false;
// }
// return true;
// }
//
// public String toString(Object[] arg0) {
// return Arrays.toString(arg0);
// }
//
// });
//
// loaderCtrl.replay();
//
// ltw.addTransformer(transformer);
// }
//
// @Test
// public void testGetThrowawayClassLoader() {
// loaderCtrl.reset();
// ClassLoader cl = new URLClassLoader(new URL[0]);
// loaderCtrl.expectAndReturn(loader.getClassLoader(), cl);
// loaderCtrl.replay();
//
// assertSame(ltw.getThrowawayClassLoader(), cl);
// }
}

View File

@@ -16,8 +16,11 @@
package org.springframework.jndi;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import javax.naming.Context;
import javax.naming.NamingException;
@@ -383,11 +386,8 @@ public class JndiObjectFactoryBeanTests {
public void testLookupWithExposeAccessContext() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
final Context mockCtx = createMock(Context.class);
expect(mockCtx.lookup("foo")).andReturn(tb);
mockCtx.close();
expectLastCall().times(2);
replay(mockCtx);
final Context mockCtx = mock(Context.class);
given(mockCtx.lookup("foo")).willReturn(tb);
jof.setJndiTemplate(new JndiTemplate() {
@Override
protected Context createInitialContext() {
@@ -406,7 +406,7 @@ public class JndiObjectFactoryBeanTests {
proxy.equals(proxy);
proxy.hashCode();
proxy.toString();
verify(mockCtx);
verify(mockCtx, times(2)).close();
}
}

View File

@@ -16,8 +16,10 @@
package org.springframework.jndi;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import javax.naming.Context;
import javax.naming.NameNotFoundException;
@@ -36,10 +38,8 @@ public class JndiTemplateTests {
public void testLookupSucceeds() throws Exception {
Object o = new Object();
String name = "foo";
final Context context = createMock(Context.class);
expect(context.lookup(name)).andReturn(o);
context.close();
replay(context);
final Context context = mock(Context.class);
given(context.lookup(name)).willReturn(o);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -50,17 +50,15 @@ public class JndiTemplateTests {
Object o2 = jt.lookup(name);
assertEquals(o, o2);
verify(context);
verify(context).close();
}
@Test
public void testLookupFails() throws Exception {
NameNotFoundException ne = new NameNotFoundException();
String name = "foo";
final Context context = createMock(Context.class);
expect(context.lookup(name)).andThrow(ne);
context.close();
replay(context);
final Context context = mock(Context.class);
given(context.lookup(name)).willThrow(ne);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -76,16 +74,14 @@ public class JndiTemplateTests {
catch (NameNotFoundException ex) {
// Ok
}
verify(context);
verify(context).close();
}
@Test
public void testLookupReturnsNull() throws Exception {
String name = "foo";
final Context context = createMock(Context.class);
expect(context.lookup(name)).andReturn(null);
context.close();
replay(context);
final Context context = mock(Context.class);
given(context.lookup(name)).willReturn(null);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -101,17 +97,15 @@ public class JndiTemplateTests {
catch (NameNotFoundException ex) {
// Ok
}
verify(context);
verify(context).close();
}
@Test
public void testLookupFailsWithTypeMismatch() throws Exception {
Object o = new Object();
String name = "foo";
final Context context = createMock(Context.class);
expect(context.lookup(name)).andReturn(o);
context.close();
replay(context);
final Context context = mock(Context.class);
given(context.lookup(name)).willReturn(o);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -127,17 +121,14 @@ public class JndiTemplateTests {
catch (TypeMismatchNamingException ex) {
// Ok
}
verify(context);
verify(context).close();
}
@Test
public void testBind() throws Exception {
Object o = new Object();
String name = "foo";
final Context context = createMock(Context.class);
context.bind(name, o);
context.close();
replay(context);
final Context context = mock(Context.class);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -147,17 +138,15 @@ public class JndiTemplateTests {
};
jt.bind(name, o);
verify(context);
verify(context).bind(name, o);
verify(context).close();
}
@Test
public void testRebind() throws Exception {
Object o = new Object();
String name = "foo";
final Context context = createMock(Context.class);
context.rebind(name, o);
context.close();
replay(context);
final Context context = mock(Context.class);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -167,16 +156,15 @@ public class JndiTemplateTests {
};
jt.rebind(name, o);
verify(context);
verify(context).rebind(name, o);
verify(context).close();
;
}
@Test
public void testUnbind() throws Exception {
String name = "something";
final Context context = createMock(Context.class);
context.unbind(name);
context.close();
replay(context);
final Context context = mock(Context.class);
JndiTemplate jt = new JndiTemplate() {
@Override
@@ -186,7 +174,8 @@ public class JndiTemplateTests {
};
jt.unbind(name);
verify(context);
verify(context).unbind(name);
verify(context).close();
}
}

View File

@@ -19,21 +19,20 @@ package org.springframework.scheduling.concurrent;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import junit.framework.AssertionFailedError;
import org.easymock.MockControl;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.task.NoOpRunnable;
import static org.mockito.Mockito.*;
/**
* @author Rick Evans
* @author Juergen Hoeller
@@ -58,11 +57,7 @@ public class ScheduledExecutorFactoryBeanTests {
@Test
@SuppressWarnings("serial")
public void testShutdownNowIsPropagatedToTheExecutorOnDestroy() throws Exception {
MockControl mockScheduledExecutorService = MockControl.createNiceControl(ScheduledExecutorService.class);
final ScheduledExecutorService executor = (ScheduledExecutorService) mockScheduledExecutorService.getMock();
executor.shutdownNow();
mockScheduledExecutorService.setReturnValue(null);
mockScheduledExecutorService.replay();
final ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
@Override
@@ -76,17 +71,13 @@ public class ScheduledExecutorFactoryBeanTests {
factory.afterPropertiesSet();
factory.destroy();
mockScheduledExecutorService.verify();
verify(executor).shutdownNow();
}
@Test
@SuppressWarnings("serial")
public void testShutdownIsPropagatedToTheExecutorOnDestroy() throws Exception {
MockControl mockScheduledExecutorService = MockControl.createNiceControl(ScheduledExecutorService.class);
final ScheduledExecutorService executor = (ScheduledExecutorService) mockScheduledExecutorService.getMock();
executor.shutdown();
mockScheduledExecutorService.setVoidCallable();
mockScheduledExecutorService.replay();
final ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
@Override
@@ -101,16 +92,12 @@ public class ScheduledExecutorFactoryBeanTests {
factory.afterPropertiesSet();
factory.destroy();
mockScheduledExecutorService.verify();
verify(executor).shutdown();
}
@Test
public void testOneTimeExecutionIsSetUpAndFiresCorrectly() throws Exception {
MockControl mockRunnable = MockControl.createControl(Runnable.class);
Runnable runnable = (Runnable) mockRunnable.getMock();
runnable.run();
mockRunnable.setVoidCallable();
mockRunnable.replay();
Runnable runnable = mock(Runnable.class);
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
@@ -120,18 +107,12 @@ public class ScheduledExecutorFactoryBeanTests {
pauseToLetTaskStart(1);
factory.destroy();
mockRunnable.verify();
verify(runnable).run();
}
@Test
public void testFixedRepeatedExecutionIsSetUpAndFiresCorrectly() throws Exception {
MockControl mockRunnable = MockControl.createControl(Runnable.class);
Runnable runnable = (Runnable) mockRunnable.getMock();
runnable.run();
mockRunnable.setVoidCallable();
runnable.run();
mockRunnable.setVoidCallable();
mockRunnable.replay();
Runnable runnable = mock(Runnable.class);
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
task.setPeriod(500);
@@ -143,18 +124,13 @@ public class ScheduledExecutorFactoryBeanTests {
pauseToLetTaskStart(2);
factory.destroy();
mockRunnable.verify();
verify(runnable, atLeast(2)).run();
}
@Test
public void testFixedRepeatedExecutionIsSetUpAndFiresCorrectlyAfterException() throws Exception {
MockControl mockRunnable = MockControl.createControl(Runnable.class);
Runnable runnable = (Runnable) mockRunnable.getMock();
runnable.run();
mockRunnable.setThrowable(new IllegalStateException());
runnable.run();
mockRunnable.setThrowable(new IllegalStateException());
mockRunnable.replay();
Runnable runnable = mock(Runnable.class);
willThrow(new IllegalStateException()).given(runnable).run();
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
task.setPeriod(500);
@@ -167,19 +143,13 @@ public class ScheduledExecutorFactoryBeanTests {
pauseToLetTaskStart(2);
factory.destroy();
mockRunnable.verify();
verify(runnable, atLeast(2)).run();
}
@Ignore
@Test
public void testWithInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectly() throws Exception {
MockControl mockRunnable = MockControl.createControl(Runnable.class);
Runnable runnable = (Runnable) mockRunnable.getMock();
runnable.run();
mockRunnable.setVoidCallable();
runnable.run();
mockRunnable.setVoidCallable();
mockRunnable.replay();
Runnable runnable = mock(Runnable.class);
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
task.setPeriod(500);
@@ -192,24 +162,15 @@ public class ScheduledExecutorFactoryBeanTests {
// invoke destroy before tasks have even been scheduled...
factory.destroy();
try {
mockRunnable.verify();
fail("Mock must never have been called");
}
catch (AssertionFailedError expected) {
}
// Mock must never have been called
verify(runnable, never()).run();
}
@Ignore
@Test
public void testWithInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectlyAfterException() throws Exception {
MockControl mockRunnable = MockControl.createControl(Runnable.class);
Runnable runnable = (Runnable) mockRunnable.getMock();
runnable.run();
mockRunnable.setThrowable(new IllegalStateException());
runnable.run();
mockRunnable.setThrowable(new IllegalStateException());
mockRunnable.replay();
Runnable runnable = mock(Runnable.class);
willThrow(new IllegalStateException()).given(runnable).run();
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
task.setPeriod(500);
@@ -223,12 +184,8 @@ public class ScheduledExecutorFactoryBeanTests {
// invoke destroy before tasks have even been scheduled...
factory.destroy();
try {
mockRunnable.verify();
fail("Mock must never have been called");
}
catch (AssertionFailedError expected) {
}
// Mock must never have been called
verify(runnable, never()).run();
}
@Test

View File

@@ -16,11 +16,13 @@
package org.springframework.scripting.bsh;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import java.util.Collection;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.target.dynamic.Refreshable;
@@ -189,14 +191,10 @@ public class BshScriptFactoryTests extends TestCase {
}
public void testScriptThatCompilesButIsJustPlainBad() throws Exception {
MockControl mock = MockControl.createControl(ScriptSource.class);
ScriptSource script = (ScriptSource) mock.getMock();
script.getScriptAsString();
ScriptSource script = mock(ScriptSource.class);
final String badScript = "String getMessage() { throw new IllegalArgumentException(); }";
mock.setReturnValue(badScript);
script.isModified();
mock.setReturnValue(true);
mock.replay();
given(script.getScriptAsString()).willReturn(badScript);
given(script.isModified()).willReturn(true);
BshScriptFactory factory = new BshScriptFactory(
ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript,
new Class<?>[] {Messenger.class});
@@ -207,7 +205,6 @@ public class BshScriptFactoryTests extends TestCase {
}
catch (BshScriptUtils.BshExecutionException expected) {
}
mock.verify();
}
public void testCtorWithNullScriptSourceLocator() throws Exception {

View File

@@ -23,6 +23,8 @@ import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import groovy.lang.DelegatingMetaClass;
import groovy.lang.GroovyObject;
@@ -30,7 +32,6 @@ import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Map;
import org.easymock.EasyMock;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
@@ -193,11 +194,10 @@ public class GroovyScriptFactoryTests {
@Test
public void testScriptedClassThatDoesNotHaveANoArgCtor() throws Exception {
ScriptSource script = EasyMock.createMock(ScriptSource.class);
ScriptSource script = mock(ScriptSource.class);
final String badScript = "class Foo { public Foo(String foo) {}}";
EasyMock.expect(script.getScriptAsString()).andReturn(badScript);
EasyMock.expect(script.suggestedClassName()).andReturn("someName");
EasyMock.replay(script);
given(script.getScriptAsString()).willReturn(badScript);
given(script.suggestedClassName()).willReturn("someName");
GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX
+ badScript);
try {
@@ -206,16 +206,14 @@ public class GroovyScriptFactoryTests {
} catch (ScriptCompilationException expected) {
assertTrue(expected.contains(InstantiationException.class));
}
EasyMock.verify(script);
}
@Test
public void testScriptedClassThatHasNoPublicNoArgCtor() throws Exception {
ScriptSource script = EasyMock.createMock(ScriptSource.class);
ScriptSource script = mock(ScriptSource.class);
final String badScript = "class Foo { protected Foo() {}}";
EasyMock.expect(script.getScriptAsString()).andReturn(badScript);
EasyMock.expect(script.suggestedClassName()).andReturn("someName");
EasyMock.replay(script);
given(script.getScriptAsString()).willReturn(badScript);
given(script.suggestedClassName()).willReturn("someName");
GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX
+ badScript);
try {
@@ -224,7 +222,6 @@ public class GroovyScriptFactoryTests {
} catch (ScriptCompilationException expected) {
assertTrue(expected.contains(IllegalAccessException.class));
}
EasyMock.verify(script);
}
@Test
@@ -290,15 +287,13 @@ public class GroovyScriptFactoryTests {
@Test
public void testGetScriptedObjectDoesNotChokeOnNullInterfacesBeingPassedIn() throws Exception {
ScriptSource script = EasyMock.createMock(ScriptSource.class);
EasyMock.expect(script.getScriptAsString()).andReturn("class Bar {}");
EasyMock.expect(script.suggestedClassName()).andReturn("someName");
EasyMock.replay(script);
ScriptSource script = mock(ScriptSource.class);
given(script.getScriptAsString()).willReturn("class Bar {}");
given(script.suggestedClassName()).willReturn("someName");
GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)");
Object scriptedObject = factory.getScriptedObject(script, null);
assertNotNull(scriptedObject);
EasyMock.verify(script);
}
@Test

View File

@@ -16,8 +16,8 @@
package org.springframework.scripting.support;
import static org.mockito.Mockito.mock;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.beans.factory.BeanFactory;
@@ -27,15 +27,12 @@ import org.springframework.beans.factory.BeanFactory;
public class RefreshableScriptTargetSourceTests extends TestCase {
public void testCreateWithNullScriptSource() throws Exception {
MockControl mockFactory = MockControl.createNiceControl(BeanFactory.class);
mockFactory.replay();
try {
new RefreshableScriptTargetSource((BeanFactory) mockFactory.getMock(), "a.bean", null, null, false);
new RefreshableScriptTargetSource(mock(BeanFactory.class), "a.bean", null, null, false);
fail("Must have failed when passed a null ScriptSource.");
}
catch (IllegalArgumentException expected) {
}
mockFactory.verify();
}
}

View File

@@ -16,12 +16,14 @@
package org.springframework.scripting.support;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
@@ -41,43 +43,29 @@ public class ResourceScriptSourceTests extends TestCase {
}
public void testDoesNotPropagateFatalExceptionOnResourceThatCannotBeResolvedToAFile() throws Exception {
MockControl mock = MockControl.createControl(Resource.class);
Resource resource = (Resource) mock.getMock();
resource.lastModified();
mock.setThrowable(new IOException());
mock.replay();
Resource resource = mock(Resource.class);
given(resource.lastModified()).willThrow(new IOException());
ResourceScriptSource scriptSource = new ResourceScriptSource(resource);
long lastModified = scriptSource.retrieveLastModifiedTime();
assertEquals(0, lastModified);
mock.verify();
}
public void testBeginsInModifiedState() throws Exception {
MockControl mock = MockControl.createControl(Resource.class);
Resource resource = (Resource) mock.getMock();
mock.replay();
Resource resource = mock(Resource.class);
ResourceScriptSource scriptSource = new ResourceScriptSource(resource);
assertTrue(scriptSource.isModified());
mock.verify();
}
public void testLastModifiedWorksWithResourceThatDoesNotSupportFileBasedReading() throws Exception {
MockControl mock = MockControl.createControl(Resource.class);
Resource resource = (Resource) mock.getMock();
Resource resource = mock(Resource.class);
// underlying File is asked for so that the last modified time can be checked...
resource.lastModified();
mock.setReturnValue(100, 2);
// And then mock the file changing; i.e. the File says it has been modified
given(resource.lastModified()).willReturn(100L, 100L, 200L);
// does not support File-based reading; delegates to InputStream-style reading...
//resource.getFile();
//mock.setThrowable(new FileNotFoundException());
resource.getInputStream();
mock.setReturnValue(new ByteArrayInputStream(new byte[0]));
// And then mock the file changing; i.e. the File says it has been modified
resource.lastModified();
mock.setReturnValue(200);
mock.replay();
given(resource.getInputStream()).willReturn(new ByteArrayInputStream(new byte[0]));
ResourceScriptSource scriptSource = new ResourceScriptSource(resource);
assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified());
@@ -85,7 +73,6 @@ public class ResourceScriptSourceTests extends TestCase {
assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified());
// Must now report back as having been modified
assertTrue("ResourceScriptSource must report back as being modified if the underlying File resource is reporting a changed lastModified time.", scriptSource.isModified());
mock.verify();
}
public void testLastModifiedWorksWithResourceThatDoesNotSupportFileBasedAccessAtAll() throws Exception {

View File

@@ -16,8 +16,8 @@
package org.springframework.scripting.support;
import static org.mockito.Mockito.mock;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.BeanFactory;
@@ -75,15 +75,12 @@ public class ScriptFactoryPostProcessorTests extends TestCase {
}
public void testThrowsExceptionIfGivenNonAbstractBeanFactoryImplementation() throws Exception {
MockControl mock = MockControl.createControl(BeanFactory.class);
mock.replay();
try {
new ScriptFactoryPostProcessor().setBeanFactory((BeanFactory) mock.getMock());
new ScriptFactoryPostProcessor().setBeanFactory(mock(BeanFactory.class));
fail("Must have thrown exception by this point.");
}
catch (IllegalStateException expected) {
}
mock.verify();
}
public void testChangeScriptWithRefreshableBeanFunctionality() throws Exception {