Commits

Micha Kops committed 19cd6b2 Draft

Initial import.

  • Participants

Comments (0)

Files changed (6)

+.project
+.settings
+.classpath
+.target
+# jUnit 4.11 Examples
+
+Some quick examples for new features in jUnit 4.11.
+
+Please feel free to take a look at [my blog] for the full tutorial.
+
+---
+
+**2012 Micha Kops / hasCode.com**
+
+   [my blog]:http://www.hascode.com/
+<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>junit-4.11-examples</artifactId>
+  <version>0.0.1</version>
+  <dependencies>
+  	<dependency>
+  		<groupId>junit</groupId>
+  		<artifactId>junit</artifactId>
+  		<version>4.11-beta-1</version>
+  	</dependency>
+  </dependencies>
+</project>

src/main/java/com/hascode/tutorial/User.java

+package com.hascode.tutorial;
+
+public class User {
+	private Long id;
+	private String name;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(final String name) {
+		this.name = name;
+	}
+
+	public User() {
+	}
+
+	public User(final Long id, final String name) {
+		this.id = id;
+		this.name = name;
+	}
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((id == null) ? 0 : id.hashCode());
+		return result;
+	}
+
+	@Override
+	public boolean equals(final Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		User other = (User) obj;
+		if (id == null) {
+			if (other.id != null)
+				return false;
+		} else if (!id.equals(other.id))
+			return false;
+		return true;
+	}
+
+	@Override
+	public String toString() {
+		return "User [id=" + id + ", name=" + name + "]";
+	}
+}

src/main/java/com/hascode/tutorial/UserDao.java

+package com.hascode.tutorial;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class UserDao {
+	final Map<Long, User> userStore = new HashMap<Long, User>();
+	{
+		userStore.put(1l, new User(1l, "Fred"));
+		userStore.put(3l, new User(3l, "Suzie"));
+		userStore.put(5l, new User(5l, "Tim"));
+		userStore.put(666l, new User(666l, "Luzi"));
+		userStore.put(1024l, new User(1024l, "Bob"));
+	}
+
+	public User findById(final Long id) {
+		return userStore.get(id);
+	}
+}

src/test/java/com/hascode/tutorial/ParameterizedTest.java

+package com.hascode.tutorial;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class ParameterizedTest {
+	@Parameters(name = "Run {index}: findUserById({0})={1}")
+	public static Iterable<Object[]> data() {
+		return Arrays.asList(new Object[][] { { 1l, new User(1l, "Fred") },
+				{ 3l, new User(3l, "Suzie") }, { 5l, new User(5l, "Tim") },
+				{ 666l, new User(666l, "Luzi") },
+				{ 1024l, new User(1025l, "Bob") } });
+	}
+
+	private final Long idParam;
+	private final User userParam;
+
+	public ParameterizedTest(final Long id, final User user) {
+		this.idParam = id;
+		this.userParam = user;
+	}
+
+	@Test
+	public void testUserMapping() {
+		UserDao userDao = new UserDao();
+		User user = userDao.findById(idParam);
+		assertEquals(idParam, user.getId());
+		assertEquals(userParam.getName(), user.getName());
+	}
+}