Commits

Micha Kops  committed 0a6fa6f

Mockito Testing Tutorial Sources added

  • Participants
  • Parent commits 9b7b0e8

Comments (0)

Files changed (13)

File mockito-tutorial/.classpath

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

File mockito-tutorial/.project

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>mockito-tutorial</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.maven.ide.eclipse.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.maven.ide.eclipse.maven2Nature</nature>
+	</natures>
+</projectDescription>

File mockito-tutorial/.settings/org.eclipse.jdt.core.prefs

+#Sun Mar 13 19:42:43 CET 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.6

File mockito-tutorial/.settings/org.maven.ide.eclipse.prefs

+#Sat Mar 12 19:56:51 CET 2011
+activeProfiles=
+eclipse.preferences.version=1
+fullBuildGoals=process-test-resources
+resolveWorkspaceProjects=true
+resourceFilterGoals=process-resources resources\:testResources
+skipCompilerPlugin=true
+version=1

File mockito-tutorial/pom.xml

+<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.hascode.tutorial</groupId>
+	<artifactId>mockito-tutorial</artifactId>
+	<version>0.0.1</version>
+	<dependencies>
+		<dependency>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-core</artifactId>
+			<version>1.8.5</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.8.2</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+</project>

File mockito-tutorial/src/main/java/com/hascode/tutorial/AccountService.java

+package com.hascode.tutorial;
+
+
+public class AccountService {
+	private final UserRepository		userRepository;
+	private final NotificationService	notificationService;
+
+	public AccountService(final UserRepository userRepository, final NotificationService notificationService) {
+		this.userRepository = userRepository;
+		this.notificationService = notificationService;
+	}
+
+	public void removeUser(final UserBean user) {
+		notificationService.notifyUser(user, getUserDeletionMessage(user));
+		userRepository.remove(user);
+	}
+
+	private String getUserDeletionMessage(final UserBean user) {
+		return String.format("Dear administrator,\n the following user account has been deleted: %s", user.getName());
+	}
+}

File mockito-tutorial/src/main/java/com/hascode/tutorial/NotificationService.java

+package com.hascode.tutorial;
+
+public interface NotificationService {
+	public abstract void notifyUser(final UserBean user, final String message);
+}

File mockito-tutorial/src/main/java/com/hascode/tutorial/UserBean.java

+package com.hascode.tutorial;
+
+import java.util.Calendar;
+
+public interface UserBean {
+	public abstract String getName();
+
+	public abstract String setName();
+
+	public abstract Calendar getLastLogin();
+
+	public abstract void setLastLogin(Calendar lastLogin);
+
+	public abstract boolean isActive();
+
+	public abstract void setActive(boolean active);
+}

File mockito-tutorial/src/main/java/com/hascode/tutorial/UserRepository.java

+package com.hascode.tutorial;
+
+import java.util.List;
+
+public interface UserRepository {
+	public abstract List<UserBean> findAll();
+
+	public abstract void save(final UserBean user);
+
+	public abstract void remove(final UserBean user);
+}

File mockito-tutorial/src/test/java/com/hascode/tutorial/SimpleMockExample.java

+package com.hascode.tutorial;
+
+import static org.mockito.Mockito.mock;
+
+import org.junit.Test;
+public class SimpleMockExample {
+	@Test public void testSomething() {
+		UserRepository userRepository = mock(UserRepository.class);
+	}
+}

File mockito-tutorial/src/test/java/com/hascode/tutorial/SimpleMockExampleWithAnnotations.java

+package com.hascode.tutorial;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+public class SimpleMockExampleWithAnnotations {
+	@Mock private UserRepository	userRepository;
+
+	@Before public void setUp() {
+		MockitoAnnotations.initMocks(this);
+	}
+
+	@Test public void testSomething() {
+		// ...
+	}
+}

File mockito-tutorial/src/test/java/com/hascode/tutorial/SimpleStubExample.java

+package com.hascode.tutorial;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import junit.framework.Assert;
+
+import org.junit.Test;
+public class SimpleStubExample {
+	@Test(expected = RuntimeException.class) public void testSomething() {
+		UserRepository userRepository = mock(UserRepository.class);
+		when(userRepository.findAll()).thenReturn(null);
+		doThrow(new RuntimeException()).when(userRepository).save(any(UserBean.class));
+		Assert.assertNull(userRepository.findAll());
+		userRepository.save(null);
+	}
+}

File mockito-tutorial/src/test/java/com/hascode/tutorial/VerificationExample.java

+package com.hascode.tutorial;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Test;
+
+public class VerificationExample {
+	@Test public void testExample() {
+		// mock
+		NotificationService notificationService = mock(NotificationService.class);
+		UserRepository userRepository = mock(UserRepository.class);
+		UserBean user = mock(UserBean.class);
+
+		// init
+		AccountService service = new AccountService(userRepository, notificationService);
+
+		// run
+		service.removeUser(user);
+
+		// verify
+		verify(notificationService, times(1)).notifyUser(any(UserBean.class), anyString()); // verify
+																							// 1
+																							// invocation
+		verify(userRepository).remove(any(UserBean.class)); // verify 1
+															// invocation, short
+															// syntax
+
+		reset(notificationService); // reset the mocks
+		reset(userRepository);
+
+		verify(notificationService, never()).notifyUser(any(UserBean.class), anyString());
+
+		service.removeUser(user);
+		service.removeUser(user);
+
+		verify(notificationService, times(2)).notifyUser(any(UserBean.class), anyString());
+		verify(userRepository, atLeast(2)).remove(any(UserBean.class));
+	}
+}