Permalink
Browse files

Merge pull request #451 from pettermahlen/cucumber-jvm-322

Making it possible to instantiate Descriptions for individual tests without an actual Java class
  • Loading branch information...
2 parents 6dbbd79 + b564894 commit 6a8215ab69317916807f246daef80a9d4cf5dc8e @dsaff dsaff committed Aug 14, 2012
@@ -36,7 +36,7 @@
* Create a <code>Description</code> named <code>name</code>.
* Generally, you will add children to this <code>Description</code>.
* @param name the name of the <code>Description</code>
- * @param annotations
+ * @param annotations meta-data about the test, for downstream interpreters
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createSuiteDescription(String name, Annotation... annotations) {
@@ -56,6 +56,20 @@ public static Description createSuiteDescription(String name, Serializable uniqu
}
/**
+ * Create a <code>Description</code> of a single test named <code>name</code> in the 'class' named
+ * <code>className</code>. Generally, this will be a leaf <code>Description</code>. This method is a better choice
+ * than {@link #createTestDescription(Class, String, Annotation...)} for test runners whose test cases are not
+ * defined in an actual Java <code>Class</code>.
+ * @param className the class name of the test
+ * @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
+ * @param annotations meta-data about the test, for downstream interpreters
+ * @return a <code>Description</code> named <code>name</code>
+ */
+ public static Description createTestDescription(String className, String name, Annotation... annotations) {
+ return new Description(String.format("%s(%s)", name, className), annotations);
+ }
+
+ /**
* Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
* Generally, this will be a leaf <code>Description</code>.
* @param clazz the class of the test
@@ -64,19 +78,30 @@ public static Description createSuiteDescription(String name, Serializable uniqu
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createTestDescription(Class<?> clazz, String name, Annotation... annotations) {
- return new Description(String.format("%s(%s)", name, clazz.getName()), annotations);
+ return createTestDescription(clazz.getName(), name, annotations);
}
/**
* Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
- * Generally, this will be a leaf <code>Description</code>.
+ * Generally, this will be a leaf <code>Description</code>.
* (This remains for binary compatibility with clients of JUnit 4.3)
* @param clazz the class of the test
* @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createTestDescription(Class<?> clazz, String name) {
- return createTestDescription(clazz, name, new Annotation[0]);
+ return createTestDescription(clazz.getName(), name);
+ }
+
+ /**
+ * Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
+ * Generally, this will be a leaf <code>Description</code>.
+ *
+ * @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
+ * @return a <code>Description</code> named <code>name</code>
+ */
+ public static Description createTestDescription(String className, String name, Serializable uniqueId) {
+ return new Description(String.format("%s(%s)", name, className), uniqueId);
}
/**
@@ -1,9 +1,15 @@
package org.junit.tests.experimental.max;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
import org.junit.Test;
import org.junit.runner.Description;
+import java.lang.annotation.Annotation;
+
public class DescriptionTest {
@Test
@@ -25,4 +31,40 @@ public void createSuiteDescription_whenZeroLength() {
public void createSuiteDescription_whenNull() {
Description.createSuiteDescription((String) null);
}
+
+ @Test
+ public void parseClassAndMethodNoAnnotations() throws Exception {
+ Description description = Description.createTestDescription(Description.class, "aTestMethod");
+
+ assertThat(description.getClassName(), equalTo("org.junit.runner.Description"));
+ assertThat(description.getMethodName(), equalTo("aTestMethod"));
+ assertThat(description.getAnnotations().size(), equalTo(0));
+ }
+
+ @Test
+ public void parseClassAndMethodWithAnnotations() throws Exception {
+ Annotation[] annotations =
+ DescriptionTest.class.getMethod("parseClassAndMethodWithAnnotations").getDeclaredAnnotations();
+
+ Description description = Description.createTestDescription(Description.class, "aTestMethod", annotations);
+
+ assertThat(description.getClassName(), equalTo("org.junit.runner.Description"));
+ assertThat(description.getMethodName(), equalTo("aTestMethod"));
+ assertThat(description.getAnnotations().size(), equalTo(1));
+ }
+
+ @Test
+ public void parseClassNameAndMethodUniqueId() throws Exception {
+ Description description = Description.createTestDescription("not a class name", "aTestMethod", 123);
+
+ assertThat(description.getClassName(), equalTo("not a class name"));
+ assertThat(description.getMethodName(), equalTo("aTestMethod"));
+ assertThat(description.getAnnotations().size(), equalTo(0));
+ }
+
+ @Test
+ public void sameNamesButDifferentUniqueIdAreNotEqual() throws Exception {
+ assertThat(Description.createTestDescription("not a class name", "aTestMethod", 1),
+ not(equalTo(Description.createTestDescription("not a class name", "aTestMethod", 2))));
+ }
}

0 comments on commit 6a8215a

Please sign in to comment.