Introduce mockito-configuration from bgpcep 16/4316/3
authorRobert Varga <rovarga@cisco.com>
Thu, 16 Jan 2014 15:33:58 +0000 (16:33 +0100)
committerRobert Varga <rovarga@cisco.com>
Thu, 16 Jan 2014 20:09:26 +0000 (21:09 +0100)
Change-Id: Id454e3ae9d121f2f75f7c9036c0042280ab6c6c9
Signed-off-by: Robert Varga <rovarga@cisco.com>
mockito-configuration/pom.xml [new file with mode: 0644]
mockito-configuration/src/main/java/org/mockito/configuration/ArgumentsExtractorVerifier.java [new file with mode: 0644]
mockito-configuration/src/main/java/org/mockito/configuration/MockitoConfiguration.java [new file with mode: 0644]
mockito-configuration/src/main/java/org/mockito/configuration/ThrowsUnstubbedMethodException.java [new file with mode: 0644]
mockito-configuration/src/main/java/org/mockito/configuration/UnstubbedMethodException.java [new file with mode: 0644]
mockito-configuration/src/main/resources/logback-test.xml [new file with mode: 0644]
mockito-configuration/src/test/java/org/mockito/configuration/ArgumentsExtractorVerifierTest.java [new file with mode: 0644]
mockito-configuration/src/test/java/org/mockito/configuration/DefaultAnswerTest.java [new file with mode: 0644]
pom.xml

diff --git a/mockito-configuration/pom.xml b/mockito-configuration/pom.xml
new file mode 100644 (file)
index 0000000..81558ea
--- /dev/null
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- vi: set et smarttab sw=4 tabstop=4: -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <parent>
+        <groupId>org.opendaylight.yangtools</groupId>
+        <artifactId>yangtools</artifactId>
+        <version>0.1.1-SNAPSHOT</version>
+    </parent>
+
+    <!-- FIXME: remove this once parent is bumped -->
+    <version>0.6.0-SNAPSHOT</version>
+
+       <modelVersion>4.0.0</modelVersion>
+       <artifactId>mockito-configuration</artifactId>
+       <description>Default mockito configuration</description>
+       <packaging>jar</packaging><!-- not needed in OSGi -->
+    <name>${project.artifactId}</name>
+       <prerequisites>
+               <maven>3.0.4</maven>
+       </prerequisites>
+
+       <dependencies>
+               <!-- all those dependencies will be in test scope as mockito-configuration should be referenced as test scope dependency -->
+               <dependency>
+                       <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>compile</scope>
+               </dependency>
+               <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+               </dependency>
+       </dependencies>
+</project>
diff --git a/mockito-configuration/src/main/java/org/mockito/configuration/ArgumentsExtractorVerifier.java b/mockito-configuration/src/main/java/org/mockito/configuration/ArgumentsExtractorVerifier.java
new file mode 100644 (file)
index 0000000..53db342
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.mockito.configuration;
+
+import org.mockito.exceptions.base.MockitoException;
+import org.mockito.internal.invocation.InvocationsFinder;
+import org.mockito.internal.verification.api.VerificationData;
+import org.mockito.invocation.Invocation;
+import org.mockito.verification.VerificationMode;
+
+import java.util.List;
+
+/**
+ * Verifier that extracts arguments from actual invocation. Useful when deeper validation of arguments is needed.
+ *
+ */
+public class ArgumentsExtractorVerifier implements VerificationMode {
+               private Object[] arguments;
+
+               @Override
+               public void verify(VerificationData data) {
+                       InvocationsFinder finder = new InvocationsFinder();
+                       List<Invocation> actualInvocations = finder.findInvocations(data.getAllInvocations(), data.getWanted());
+                       if (actualInvocations.size() != 1) {
+                               throw new MockitoException("This verifier can only be used with 1 invocation, got " + actualInvocations.size());
+                       }
+                       Invocation invocation = actualInvocations.get(0);
+                       arguments = invocation.getArguments();
+                       invocation.markVerified();
+
+               }
+               public Object[] getArguments(){
+                       return arguments;
+               }
+       }
diff --git a/mockito-configuration/src/main/java/org/mockito/configuration/MockitoConfiguration.java b/mockito-configuration/src/main/java/org/mockito/configuration/MockitoConfiguration.java
new file mode 100644 (file)
index 0000000..6c783e0
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.mockito.configuration;
+
+import org.mockito.stubbing.Answer;
+
+/**
+ * Configuration customization for Mockito. Change default answer to {@link ThrowsUnstubbedMethodException}.
+ */
+public class MockitoConfiguration extends DefaultMockitoConfiguration {
+
+       @Override
+       public Answer<Object> getDefaultAnswer() {
+               return new ThrowsUnstubbedMethodException();
+       }
+}
diff --git a/mockito-configuration/src/main/java/org/mockito/configuration/ThrowsUnstubbedMethodException.java b/mockito-configuration/src/main/java/org/mockito/configuration/ThrowsUnstubbedMethodException.java
new file mode 100644 (file)
index 0000000..83262bc
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.mockito.configuration;
+
+import java.io.Serializable;
+
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+/**
+ * Answer that throws {@link UnstubbedMethodException}.
+ */
+public class ThrowsUnstubbedMethodException implements Answer<Object>, Serializable {
+       private static final long serialVersionUID = 1L;
+
+    public ThrowsUnstubbedMethodException() {
+    }
+
+    @Override
+       public Object answer(InvocationOnMock invocation) throws Throwable {
+        Throwable t = new UnstubbedMethodException(invocation.toString() + " was not stubbed");
+        throw t;
+    }
+}
diff --git a/mockito-configuration/src/main/java/org/mockito/configuration/UnstubbedMethodException.java b/mockito-configuration/src/main/java/org/mockito/configuration/UnstubbedMethodException.java
new file mode 100644 (file)
index 0000000..ac74866
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.mockito.configuration;
+
+
+/**
+ * Exception to be thrown on unstubbed method call.
+ */
+public class UnstubbedMethodException extends RuntimeException {
+
+       private static final long serialVersionUID = 1L;
+
+       public UnstubbedMethodException(String message) {
+               super(message);
+       }
+
+}
diff --git a/mockito-configuration/src/main/resources/logback-test.xml b/mockito-configuration/src/main/resources/logback-test.xml
new file mode 100644 (file)
index 0000000..2838411
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+    <encoder>
+      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
+    </encoder>
+  </appender>
+
+  <root level="DEBUG">
+    <appender-ref ref="STDOUT" />
+  </root>
+</configuration>
diff --git a/mockito-configuration/src/test/java/org/mockito/configuration/ArgumentsExtractorVerifierTest.java b/mockito-configuration/src/test/java/org/mockito/configuration/ArgumentsExtractorVerifierTest.java
new file mode 100644 (file)
index 0000000..ba3a41b
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.mockito.configuration;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class ArgumentsExtractorVerifierTest {
+
+       @Mock
+       List<String> mockedList;
+
+       @Test
+       public void test() {
+               MockitoAnnotations.initMocks(this);
+               doReturn(true).when(this.mockedList).add(any(String.class));
+               final String argument = "something";
+               this.mockedList.add(argument);
+               // retrieve argument
+               final ArgumentsExtractorVerifier argumentsExtractorVerifier = new ArgumentsExtractorVerifier();
+               verify(this.mockedList, argumentsExtractorVerifier).add(any(String.class));
+               assertArrayEquals(new Object[] { argument }, argumentsExtractorVerifier.getArguments());
+       }
+
+}
diff --git a/mockito-configuration/src/test/java/org/mockito/configuration/DefaultAnswerTest.java b/mockito-configuration/src/test/java/org/mockito/configuration/DefaultAnswerTest.java
new file mode 100644 (file)
index 0000000..c2019ed
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.mockito.configuration;
+
+import static org.junit.Assert.*;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class DefaultAnswerTest {
+
+       @Test
+       public void testAnswering() throws IOException {
+               Closeable mock = Mockito.mock(Closeable.class);
+               try {
+                       mock.close();
+                       fail();
+               } catch (UnstubbedMethodException e) {
+                       assertEquals("closeable.close(); was not stubbed", e.getMessage());
+               }
+       }
+
+
+
+}
diff --git a/pom.xml b/pom.xml
index 4aebc8d74600fa844704cde21b1fb8c37774b3e7..5f72c52b5edccea00e94a57d198d522381c70057 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,8 @@
         <slf4j.version>1.7.2</slf4j.version>\r
         <guava.version>14.0.1</guava.version>\r
         <xtend.version>2.4.3</xtend.version>\r
-        <groovy.version>2.1.6</groovy.version>\r
+       <groovy.version>2.1.6</groovy.version>\r
+       <mockito.version>1.9.5</mockito.version>\r
     </properties>\r
 \r
     <scm>\r
@@ -42,7 +43,8 @@
         <module>yang</module>\r
         <module>code-generator</module>\r
         <module>model</module>\r
-        <module>restconf</module>\r
+       <module>restconf</module>\r
+       <module>mockito-configuration</module>\r
         <!-- module>third-party</module -->\r
     </modules>\r
 \r
             <dependency>\r
                 <groupId>org.mockito</groupId>\r
                 <artifactId>mockito-all</artifactId>\r
-                <version>1.9.5</version>\r
+               <version>${mockito.version}</version>\r
+                <scope>test</scope>\r
+            </dependency>\r
+            <dependency>\r
+                <groupId>org.mockito</groupId>\r
+                <artifactId>mockito-core</artifactId>\r
+               <version>${mockito.version}</version>\r
                 <scope>test</scope>\r
             </dependency>\r
 \r