Micha Kops avatar Micha Kops committed 07c63bc

initial import

Comments (0)

Files changed (6)

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>junit-rules</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+	</natures>
+</projectDescription>
+<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-rules</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <dependencies>
+  	<dependency>
+  		<groupId>junit</groupId>
+  		<artifactId>junit</artifactId>
+  		<version>4.10</version>
+  	</dependency>
+  </dependencies>
+</project>

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

+package com.hascode.tutorial;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.METHOD })
+public @interface ConsoleOut {
+	boolean value() default true;
+}

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

+package com.hascode.tutorial;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class ConsoleOutRule implements TestRule {
+	private final String note;
+
+	public ConsoleOutRule(final String note) {
+		this.note = note;
+	}
+
+	public Statement apply(final Statement base, final Description description) {
+		System.out.println("rule applied. note: " + note);
+		return base;
+	}
+
+}

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

+package com.hascode.tutorial;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+public class SomeUnitTest {
+	@Rule
+	public ConsoleOutRule rule = new ConsoleOutRule("somewhere");
+
+	@Test
+	@ConsoleOut
+	public void testSomeMethod() {
+		System.out.println("test started");
+		assertTrue(true);
+	}
+}

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

+package com.hascode.tutorial;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+
+public class SomeUnitTestWithRuleChain {
+	@Rule
+	public TestRule chain = RuleChain.outerRule(new ConsoleOutRule("outer"))
+			.around(new ConsoleOutRule("middle"))
+			.around(new ConsoleOutRule("inner"));
+
+	@Test
+	@ConsoleOut
+	public void testSomeMethod() {
+		System.out.println("test started");
+		assertTrue(true);
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.