<version>2.2</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymock</artifactId>
- <version>2.3</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymockclassextension</artifactId>
- <version>2.3</version>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<artifactId>mysql-connector-java</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.3</version>
+ <scope>test</scope>
+ </dependency>
+
<!-- dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- */
+ */
package org.wamblee.io;
-import org.apache.oro.io.AwkFilenameFilter;
-import org.easymock.EasyMock;
-
+import static org.mockito.Mockito.*;
import junit.framework.TestCase;
+import org.apache.oro.io.AwkFilenameFilter;
+import org.wamblee.test.ResettableMock;
+
public class DirectoryMonitorTest extends TestCase {
private static final String REGEX = "^.*\\.txt$";
private static final String FILE1 = "file1.txt";
private TestData _data;
- private DirectoryMonitor.Listener _listener;
+ private ResettableMock<DirectoryMonitor.Listener> _listener;
+
private DirectoryMonitor _monitor;
@Override
super.setUp();
_data = new TestData(this);
_data.clean();
- _listener = EasyMock.createStrictMock(DirectoryMonitor.Listener.class);
+ _listener = new ResettableMock<DirectoryMonitor.Listener>(
+ DirectoryMonitor.Listener.class);
_monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
- REGEX), _listener);
+ REGEX), _listener.getProxy());
}
public void testEmptyDir() {
// Nothing is expected to be called.
for (int i = 0; i < 10; i++) {
- EasyMock.replay(_listener);
_monitor.poll();
- EasyMock.verify(_listener);
- EasyMock.reset(_listener);
+ verifyNoMoreInteractions(_listener.getMock());
}
}
public void testFileCreated() {
- _listener.fileCreated(_data.getFile(FILE1));
- EasyMock.replay(_listener);
_data.createFile(FILE1, "hello");
_monitor.poll();
- EasyMock.verify(_listener);
+ verify(_listener.getMock()).fileCreated(_data.getFile(FILE1));
}
public void testFileDeleted() {
_data.createFile(FILE1, "hello");
_monitor.poll();
-
- EasyMock.reset(_listener);
+ _listener.reset();
_data.deleteFile(FILE1);
- _listener.fileDeleted(_data.getFile(FILE1));
- EasyMock.replay(_listener);
_monitor.poll();
- EasyMock.verify(_listener);
+
+ verify(_listener.getMock()).fileDeleted(_data.getFile(FILE1));
+ verifyNoMoreInteractions(_listener.getMock());
}
-
- public void testFileChanged() throws InterruptedException {
+
+ public void testFileChanged() throws InterruptedException {
_data.createFile(FILE1, "hello");
_monitor.poll();
- EasyMock.reset(_listener);
-
+ _listener.reset();
+
Thread.sleep(2000);
_data.deleteFile(FILE1);
_data.createFile(FILE1, "bla");
-
- _listener.fileChanged(_data.getFile(FILE1));
- EasyMock.replay(_listener);
+
_monitor.poll();
- EasyMock.verify(_listener);
+ verify(_listener.getMock()).fileChanged(_data.getFile(FILE1));
+ verifyNoMoreInteractions(_listener.getMock());
}
-
+
public void testFileFilterIsUsed() {
- _monitor.poll();
-
+ _monitor.poll();
+
_data.createFile("file.xml", "hello");
- EasyMock.replay(_listener);
_monitor.poll();
- EasyMock.verify(_listener);
+ verifyNoMoreInteractions(_listener.getMock());
}
-
- public void testDirectoryIsIgnored() {
+
+ public void testDirectoryIsIgnored() {
_monitor.poll();
_data.createDir(FILE1);
- EasyMock.replay(_listener);
_monitor.poll();
- EasyMock.verify(_listener);
+ verifyNoMoreInteractions(_listener.getMock());
}
-
+
public void testExceptionsWIllLeadToRepeatedNotifications() {
_monitor.poll();
_data.createFile(FILE1, "hello");
- _listener.fileCreated(_data.getFile(FILE1));
- EasyMock.expectLastCall().andThrow(new RuntimeException());
- EasyMock.replay(_listener);
+ stubVoid(_listener.getMock()).toThrow(new RuntimeException()).on().
+ fileCreated(_data.getFile(FILE1));
+
try {
_monitor.poll();
} catch (RuntimeException e) {
- EasyMock.verify(_listener);
- EasyMock.reset(_listener);
+ _listener.reset();
- // polling again should lead to the same filecreated call.
- // this time no exception is thrown.
+ // polling again should lead to the same filecreated call.
+ // this time no exception is thrown.
- _listener.fileCreated(_data.getFile(FILE1));
- EasyMock.replay(_listener);
_monitor.poll();
- EasyMock.verify(_listener);
+ verify(_listener.getMock()).fileCreated(_data.getFile(FILE1));
+ verifyNoMoreInteractions(_listener.getMock());
return;
}
- fail(); // should not get here.
+ fail(); // should not get here.
}
package org.wamblee.observer;
-import static org.easymock.EasyMock.createControl;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
+import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.List;
-import junit.framework.TestCase;
+import org.mockito.InOrder;
-import org.easymock.IMocksControl;
+import junit.framework.TestCase;
/**
* Test of the observer pattern implementation.
* Tests subscription and notification of one subscriber.
*/
public void testOneObserver() {
- final Observer mockObserver = createStrictMock(Observer.class);
+ final Observer mockObserver = mock(Observer.class);
+ InOrder order = inOrder(mockObserver);
+
long subscription = _observable.subscribe(mockObserver);
assertEquals(1, _observable.getObserverCount());
final String message = "hallo";
- mockObserver.send(_observed, message);
- replay(mockObserver);
-
_observable.send(message);
- verify(mockObserver);
-
+
+ order.verify(mockObserver).send(_observed, message);
+ verifyNoMoreInteractions(mockObserver);
+
_observable.unsubscribe(subscription);
assertEquals(0, _observable.getObserverCount());
_observable.send(message);
-
+ verifyNoMoreInteractions(mockObserver);
}
/**
public void testManySubscribers() {
final int nsubscribers = SUBSCRIBER_COUNT;
final Observer[] mocks = new Observer[nsubscribers];
-
- IMocksControl control = createControl();
+ final InOrder[] order = new InOrder[nsubscribers];
List<Long> subscriptions = new ArrayList<Long>();
for (int i = 0; i < nsubscribers; i++) {
- mocks[i] = control.createMock("mock" + i, Observer.class);
+ mocks[i] = mock(Observer.class);
+ order[i] = inOrder(mocks[i]);
long subscription = _observable.subscribe(mocks[i]);
assertTrue(subscriptions.add(subscription));
}
final String message = "hallo";
+ _observable.send(message);
for (int i = 0; i < nsubscribers; i++) {
- mocks[i].send(_observed, message);
+ order[i].verify(mocks[i]).send(_observed, message);
}
- control.replay();
-
- _observable.send(message);
- control.verify();
for (int i = nsubscribers / 2; i < nsubscribers; i++) {
_observable.unsubscribe(subscriptions.get(i));
assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
_observable.getObserverCount());
- control.reset();
final String message2 = "blabla";
- for (int i = 0; i < nsubscribers / 2; i++) {
- mocks[i].send(_observed, message2);
- }
- control.replay();
-
_observable.send(message2);
- control.verify();
+ for (int i = 0; i < nsubscribers / 2; i++) {
+ order[i].verify(mocks[i]).send(_observed, message2);
+ }
+ for (int i = nsubscribers/2; i < nsubscribers; i++) {
+ verifyNoMoreInteractions(mocks[i]);
+ }
+
}
/**
*
*/
public void testUnsubscribeWithWrongSubscription() {
- Observer<Integer, String> observer = createMock(Observer.class);
- replay(observer);
-
+ Observer<Integer, String> observer = mock(Observer.class);
+
long subscription = _observable.subscribe(observer);
assertEquals(1, _observable.getObserverCount());
+++ /dev/null
-/*
- * Copyright 2008 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.test;
-
-import org.easymock.IArgumentMatcher;
-import org.easymock.classextension.EasyMock;
-
-/**
- * Some general matchers for easy mock.
- *
- * @author Erik Brakkee
- */
-public class EasyMockMatchers {
-
- public static class Any implements IArgumentMatcher {
- public Any() {
- // Empty
- }
-
- @Override
- public boolean matches(Object aArg0) {
- return true;
- }
-
- @Override
- public void appendTo(StringBuffer aBuf) {
- aBuf.append("anyObject()");
- }
- }
-
- /**
- * Type-safe matcher to match any object of a given type.
- * @param <T>
- * @param aType Type.
- * @return Returns null.
- */
- public static <T> T anyObject(Class<T> aType) {
- EasyMock.reportMatcher(new Any());
- return null;
- }
-}
--- /dev/null
+/**
+ *
+ */
+package org.wamblee.test;
+
+import static org.mockito.Mockito.*;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+/**
+ * Resettable mock is a utility to support reset functionality for mockito.
+ * @author Erik Brakkee
+ *
+ * @param <T>
+ */
+public class ResettableMock<T> {
+
+ private static class MockitoInvocationHandler<T> implements
+ InvocationHandler {
+
+ private Class<T> _class;
+ private T _mock;
+
+ public MockitoInvocationHandler(Class<T> aClass) {
+ _class = aClass;
+ _mock = mock(aClass);
+ }
+
+ public void reset() {
+ _mock = mock(_class);
+ }
+
+ public T getMock() {
+ return _mock;
+ }
+
+ @Override
+ public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
+ throws Throwable {
+ return aMethod.invoke(_mock, aArgs);
+ }
+ }
+
+ /**
+ * Invocation handler that delegates to the current mockito mock and allows
+ * creation of a new mock.
+ */
+ private ResettableMock.MockitoInvocationHandler<T> _handler;
+
+ /**
+ * Proxy object to be passed to tested classes.
+ */
+ private T _proxy;
+
+ /**
+ * Constructs the resettable mock.
+ * @param aType Type to mock. Must be an interface type.
+ */
+ public ResettableMock(Class<T> aType) {
+ if ( !aType.isInterface()) {
+ throw new IllegalArgumentException("Class '" + aType.getName() + "' must be an interface");
+ }
+ _handler = new MockitoInvocationHandler(aType);
+ _proxy = (T) Proxy.newProxyInstance(aType.getClassLoader(),
+ new Class[] { aType }, _handler);
+ }
+
+ /**
+ * Resets the mock effectively forgetting all previous interactions and verifications.
+ */
+ public void reset() {
+ _handler.reset();
+ }
+
+ /**
+ * Gets the current mock object to pass to mockito calls.
+ *
+ * @return Mock object.
+ */
+ public T getMock() {
+ return _handler.getMock();
+ }
+
+ /**
+ * Returns the proxy that your tested classes will used.
+ *
+ * @return Proxy object.
+ */
+ public T getProxy() {
+ return _proxy;
+ }
+
+}
\ No newline at end of file
*/
package org.wamblee.system.container;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
-import org.easymock.classextension.ConstructorArgs;
-import org.easymock.classextension.EasyMock;
-import org.easymock.classextension.IMocksControl;
import org.wamblee.general.Pair;
import org.wamblee.system.core.Component;
import org.wamblee.system.core.DefaultProvidedInterface;
import org.wamblee.system.core.StringComponent;
import org.wamblee.system.core.SystemAssemblyException;
import org.wamblee.test.AssertionUtils;
-import org.wamblee.test.EasyMockMatchers;
import org.wamblee.test.EventTracker;
public class ContainerTest extends TestCase {
public void testEnvironmentApplicationRollbackOnException()
throws Exception {
- IMocksControl control = EasyMock.createStrictControl();
-
Environment environment = new Environment(_tracker);
- Application application = control.createMock(Application.class,
- new ConstructorArgs(Application.class.getConstructor()),
- Application.class.getDeclaredMethod("doStart", Scope.class));
-
- application.doStart(EasyMockMatchers.anyObject(Scope.class));
- EasyMock.expectLastCall().andThrow(new RuntimeException());
- control.replay();
-
+ Application application = new Application() {
+ @Override
+ public Object doStart(Scope aScope) {
+ throw new RuntimeException();
+ }
+ };
+
try {
Container container = new Container("root", new Component[] {
environment, application }, new ProvidedInterface[0],
public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
throws Exception {
- IMocksControl control = EasyMock.createControl();
Environment environment = new Environment(_tracker);
// Application 1 will throw an exception while stopping.
- Application application1 = control.createMock(Application.class,
- new ConstructorArgs(Application.class.getConstructor()),
- Application.class.getDeclaredMethod("doStop", Object.class));
-
- application1.doStop(EasyMock.anyObject());
- EasyMock.expectLastCall().andThrow(new RuntimeException());
+ Application application1 = new Application("app1") {
+ @Override
+ public void doStop(Object aRuntime) {
+ throw new RuntimeException();
+ }
+ };
// application 2 will throw an exception while starting
- Application application2 = control.createMock(Application.class,
- new ConstructorArgs(Application.class
- .getConstructor(String.class), "application2"),
- Application.class.getDeclaredMethod("doStart", Scope.class));
-
- application2.doStart(EasyMockMatchers.anyObject(Scope.class));
- EasyMock.expectLastCall().andThrow(new RuntimeException());
-
- control.replay();
+ Application application2 = new Application("app2") {
+ public Object doStart(Scope aScope) {
+ throw new RuntimeException();
+ }
+ };
try {
Container container = new Container("root", new Component[] {
import junit.framework.TestCase;
-import org.easymock.classextension.EasyMock;
-import org.easymock.classextension.IMocksControl;
-import static org.easymock.classextension.EasyMock.*;
+import static org.mockito.Mockito.*;
+
import org.wamblee.system.container.Application;
import org.wamblee.system.core.Component;
import org.wamblee.system.core.Environment;
}
private void configureRestriction(EdgeFilter base, boolean aResult) {
- base.isViolated( (Edge)EasyMock.anyObject());
- EasyMock.expectLastCall().andReturn(aResult);
+ stub(base.isViolated((Edge)anyObject())).toReturn(aResult);
}
public void testOneRestriction() {
- IMocksControl control = EasyMock.createStrictControl();
-
- EdgeFilter base = control.createMock(EdgeFilter.class);
+ EdgeFilter base = mock(EdgeFilter.class);
CompositeEdgeFilter composite = new CompositeEdgeFilter();
composite.add(base);
configureRestriction(base, false);
- control.replay();
assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
_env, _env.getProvidedInterfaces().get(0))));
- control.verify();
// Second let the base return true and verify the result.
- control.reset();
configureRestriction(base, true);
- control.replay();
assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
_env, _env.getProvidedInterfaces().get(0))));
- control.verify();
}
public void testTwoRestrictions() {
- IMocksControl control = EasyMock.createStrictControl();
-
- EdgeFilter base1 = control.createMock(EdgeFilter.class);
+ EdgeFilter base1 = mock(EdgeFilter.class);
CompositeEdgeFilter composite = new CompositeEdgeFilter();
composite.add(base1);
- EdgeFilter base2 = control.createMock(EdgeFilter.class);
+ EdgeFilter base2 = mock(EdgeFilter.class);
composite.add(base2);
// 1. base1 not violated and base 2 not violated -> not violated.
configureRestriction(base1, false);
configureRestriction(base2, false);
- control.replay();
assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
_env, _env.getProvidedInterfaces().get(0))));
- control.verify();
- control.reset();
// 2. base 1 not violated but base 2 violated -> violated
configureRestriction(base1, false);
configureRestriction(base2, true);
- control.replay();
+
assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
_env, _env.getProvidedInterfaces().get(0))));
- control.verify();
- control.reset();
-
+
// 3. base 1 violated -> violated and base 2 not called.
configureRestriction(base1, true);
// base 2 should not be called.
- control.replay();
+
assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
_env, _env.getProvidedInterfaces().get(0))));
- control.verify();
- control.reset();
}
}
import java.util.Arrays;
import java.util.List;
-import org.easymock.classextension.EasyMock;
+import static org.mockito.Mockito.*;
import org.wamblee.test.AssertionUtils;
+
import junit.framework.TestCase;
public class GraphTest extends TestCase {
graph.addNode(x);
graph.addNode(y);
graph.addEdge(e);
- Visitor visitor = EasyMock.createMock(Visitor.class);
- visitor.visitNode(x);
- visitor.visitNode(y);
- visitor.visitEdge(e);
- EasyMock.replay(visitor);
+ Visitor visitor = mock(Visitor.class);
+
graph.accept(visitor);
- EasyMock.verify(visitor);
+ verify(visitor).visitNode(x);
+ verify(visitor).visitNode(y);
+ verify(visitor).visitEdge(e);
+
+ verifyNoMoreInteractions(visitor);
}
}