Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Created plugin for Mockito

  • Loading branch information...
commit e9e6d728a357f8ed0caebd1c2ca239f8cfc38e81 1 parent 5fe23b7
@vasiliygagin authored
View
4 junitrunner-mockito/.gitignore
@@ -0,0 +1,4 @@
+.settings
+.classpath
+.project
+target
View
33 junitrunner-mockito/pom.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.github.junitrunner</groupId>
+ <artifactId>junitrunner-mockito</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>com.github.junitrunner</groupId>
+ <artifactId>junitrunner</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.9.5</version>
+ </dependency>
+ </dependencies>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-core</artifactId>
+ <version>1.3</version>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+</project>
View
23 junitrunner-mockito/src/main/java/com/github/junitrunner/mockito/MockitoInitStatement.java
@@ -0,0 +1,23 @@
+package com.github.junitrunner.mockito;
+
+import org.junit.runners.model.Statement;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class MockitoInitStatement extends Statement {
+
+ private final Statement statement;
+ private final Object testObject;
+
+ public MockitoInitStatement(Statement statement, Object testObject) {
+ this.statement = statement;
+ this.testObject = testObject;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ MockitoAnnotations.initMocks(testObject);
+ statement.evaluate();
+ Mockito.validateMockitoUsage();
+ }
+}
View
17 junitrunner-mockito/src/main/java/com/github/junitrunner/mockito/MockitoPlugin.java
@@ -0,0 +1,17 @@
+package com.github.junitrunner.mockito;
+
+import org.junit.runners.model.Statement;
+
+import com.github.junitrunner.JUnitBasePlugin;
+import com.github.junitrunner.JUnitTest;
+
+public class MockitoPlugin extends JUnitBasePlugin {
+
+ @SuppressWarnings("unused")
+ public MockitoPlugin(Class<?> testClass) {
+ }
+
+ public Statement enhanceTest(JUnitTest test, final Object testObject, final Statement statement) {
+ return new MockitoInitStatement(statement, testObject);
+ }
+}
View
32 junitrunner-mockito/src/main/java/com/github/junitrunner/mockito/MockitoRunner.java
@@ -0,0 +1,32 @@
+package com.github.junitrunner.mockito;
+
+import java.util.List;
+
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.model.InitializationError;
+import org.mockito.internal.runners.util.FrameworkUsageValidator;
+
+import com.github.junitrunner.JUnitPlugin;
+import com.github.junitrunner.JUnitRunner;
+
+public class MockitoRunner extends JUnitRunner {
+
+ public MockitoRunner(Class<?> klass) throws InitializationError {
+ super(klass);
+ }
+
+ @Override
+ protected List<JUnitPlugin> discoverPlugins() throws InitializationError {
+ List<JUnitPlugin> plugins = super.discoverPlugins();
+ plugins.add(new MockitoPlugin(testClass));
+ return plugins;
+ }
+
+ @Override
+ public void run(final RunNotifier notifier) {
+ // add listener that validates framework usage at the end of each test
+ notifier.addListener(new FrameworkUsageValidator(notifier));
+
+ super.run(notifier);
+ }
+}
View
29 junitrunner-mockito/src/test/java/com/github/junitrunner/mockito/Mockito_Plugin_Test.java
@@ -0,0 +1,29 @@
+package com.github.junitrunner.mockito;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+import com.github.junitrunner.JUnitRunner;
+import com.github.junitrunner.WithPlugins;
+
+@RunWith(JUnitRunner.class)
+@WithPlugins(MockitoPlugin.class)
+public class Mockito_Plugin_Test {
+
+ @Mock
+ private List<String> list;
+
+ @Test
+ public void test() {
+
+ Mockito.when(list.get(0)).thenReturn("Here");
+ assertThat(list.get(0), CoreMatchers.is("Here"));
+ }
+}
View
25 junitrunner-mockito/src/test/java/com/github/junitrunner/mockito/Mockito_Runner_Test.java
@@ -0,0 +1,25 @@
+package com.github.junitrunner.mockito;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+@RunWith(MockitoRunner.class)
+public class Mockito_Runner_Test {
+
+ @Mock
+ private List<String> list;
+
+ @Test
+ public void test() {
+
+ Mockito.when(list.get(0)).thenReturn("Here");
+ assertThat(list.get(0), CoreMatchers.is("Here"));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.