[testng] 263/355: Rework invocationcount tests
Eugene Zhukov
eugene-guest at moszumanska.debian.org
Tue Aug 18 10:20:13 UTC 2015
This is an automated email from the git hooks/post-receive script.
eugene-guest pushed a commit to annotated tag OpenBSD
in repository testng.
commit ecb4e084c42966aff1256b2253947c13cfa8bb01
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date: Wed May 6 23:30:11 2015 +0200
Rework invocationcount tests
---
src/test/java/test/InvokedMethodNameListener.java | 26 ++++++
src/test/java/test/invocationcount/Base.java | 33 -------
.../test/invocationcount/DataProviderBase.java | 2 +-
.../DataProviderFalseFalseTest.java | 8 +-
.../invocationcount/DataProviderFalseTrueTest.java | 8 +-
.../invocationcount/DataProviderTrueFalseTest.java | 8 +-
.../invocationcount/DataProviderTrueTrueTest.java | 8 +-
.../test/invocationcount/FirstAndLastTimeTest.java | 104 ++++++++++++++-------
.../java/test/invocationcount/InvocationBase.java | 2 +-
.../InvocationCountFalseFalseTest.java | 8 +-
.../InvocationCountFalseTrueTest.java | 8 +-
.../InvocationCountTrueFalseTest.java | 8 +-
.../InvocationCountTrueTrueTest.java | 8 +-
13 files changed, 113 insertions(+), 118 deletions(-)
diff --git a/src/test/java/test/InvokedMethodNameListener.java b/src/test/java/test/InvokedMethodNameListener.java
new file mode 100644
index 0000000..69e70e3
--- /dev/null
+++ b/src/test/java/test/InvokedMethodNameListener.java
@@ -0,0 +1,26 @@
+package test;
+
+import org.testng.IInvokedMethod;
+import org.testng.IInvokedMethodListener;
+import org.testng.ITestResult;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class InvokedMethodNameListener implements IInvokedMethodListener {
+
+ private final List<String> invokedMethodNames = new ArrayList<>();
+
+ @Override
+ public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
+ invokedMethodNames.add(method.getTestMethod().getConstructorOrMethod().getName());
+ }
+
+ @Override
+ public void afterInvocation(IInvokedMethod method, ITestResult testResult) { }
+
+ public List<String> getInvokedMethodNames() {
+ return Collections.unmodifiableList(invokedMethodNames);
+ }
+}
diff --git a/src/test/java/test/invocationcount/Base.java b/src/test/java/test/invocationcount/Base.java
deleted file mode 100644
index 5d81d47..0000000
--- a/src/test/java/test/invocationcount/Base.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package test.invocationcount;
-
-import org.testng.annotations.BeforeClass;
-
-public class Base {
- private static int m_beforeCount;
- private static int m_afterCount;
-
- @BeforeClass
- public void init() {
-// System.out.println("RESETTING COUNTS");
- m_beforeCount = 0;
- m_afterCount = 0;
- }
-
- protected void incrementBefore() {
- m_beforeCount++;
-// System.out.println("INC BEFORE:" + m_beforeCount);
- }
-
- protected void incrementAfter() {
- m_afterCount++;
-// System.out.println("INC AFTER:" + m_beforeCount);
- }
-
- public static int getBeforeCount() {
- return m_beforeCount;
- }
-
- public static int getAfterCount() {
- return m_afterCount;
- }
-}
diff --git a/src/test/java/test/invocationcount/DataProviderBase.java b/src/test/java/test/invocationcount/DataProviderBase.java
index e3d03b8..65dea0f 100644
--- a/src/test/java/test/invocationcount/DataProviderBase.java
+++ b/src/test/java/test/invocationcount/DataProviderBase.java
@@ -3,7 +3,7 @@ package test.invocationcount;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
-public class DataProviderBase extends Base {
+public class DataProviderBase {
@Test(dataProvider = "dp")
public void f(Integer n) {
}
diff --git a/src/test/java/test/invocationcount/DataProviderFalseFalseTest.java b/src/test/java/test/invocationcount/DataProviderFalseFalseTest.java
index ba4ea74..20fbe1e 100644
--- a/src/test/java/test/invocationcount/DataProviderFalseFalseTest.java
+++ b/src/test/java/test/invocationcount/DataProviderFalseFalseTest.java
@@ -5,12 +5,8 @@ import org.testng.annotations.BeforeMethod;
public class DataProviderFalseFalseTest extends DataProviderBase {
@BeforeMethod(firstTimeOnly = false)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = false)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
diff --git a/src/test/java/test/invocationcount/DataProviderFalseTrueTest.java b/src/test/java/test/invocationcount/DataProviderFalseTrueTest.java
index a750ed1..ff1f0a5 100644
--- a/src/test/java/test/invocationcount/DataProviderFalseTrueTest.java
+++ b/src/test/java/test/invocationcount/DataProviderFalseTrueTest.java
@@ -5,13 +5,9 @@ import org.testng.annotations.BeforeMethod;
public class DataProviderFalseTrueTest extends DataProviderBase {
@BeforeMethod(firstTimeOnly = false)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = true)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
diff --git a/src/test/java/test/invocationcount/DataProviderTrueFalseTest.java b/src/test/java/test/invocationcount/DataProviderTrueFalseTest.java
index ebe1706..38c8ad7 100644
--- a/src/test/java/test/invocationcount/DataProviderTrueFalseTest.java
+++ b/src/test/java/test/invocationcount/DataProviderTrueFalseTest.java
@@ -5,12 +5,8 @@ import org.testng.annotations.BeforeMethod;
public class DataProviderTrueFalseTest extends DataProviderBase {
@BeforeMethod(firstTimeOnly = true)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = false)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
diff --git a/src/test/java/test/invocationcount/DataProviderTrueTrueTest.java b/src/test/java/test/invocationcount/DataProviderTrueTrueTest.java
index 8414c8c..e21706f 100644
--- a/src/test/java/test/invocationcount/DataProviderTrueTrueTest.java
+++ b/src/test/java/test/invocationcount/DataProviderTrueTrueTest.java
@@ -5,13 +5,9 @@ import org.testng.annotations.BeforeMethod;
public class DataProviderTrueTrueTest extends DataProviderBase {
@BeforeMethod(firstTimeOnly = true)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = true)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
diff --git a/src/test/java/test/invocationcount/FirstAndLastTimeTest.java b/src/test/java/test/invocationcount/FirstAndLastTimeTest.java
index 82c4085..8d31a06 100644
--- a/src/test/java/test/invocationcount/FirstAndLastTimeTest.java
+++ b/src/test/java/test/invocationcount/FirstAndLastTimeTest.java
@@ -1,11 +1,14 @@
package test.invocationcount;
-import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.Test;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
+import java.util.List;
+
+import test.InvokedMethodNameListener;
+import test.SimpleBaseTest;
+
+import static org.assertj.core.api.Assertions.assertThat;
/**
* Test various combination of @BeforeMethod(firstTimeOnly = true/false) and
@@ -14,72 +17,103 @@ import java.lang.reflect.Method;
* @author cbeust at google.com
*
*/
-public class FirstAndLastTimeTest {
+public class FirstAndLastTimeTest extends SimpleBaseTest {
@Test
public void verifyDataProviderFalseFalse() {
- run(DataProviderFalseFalseTest.class, 3, 3);
+ List<String> invokedMethodNames = run(DataProviderFalseFalseTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f", "afterMethod",
+ "beforeMethod", "f", "afterMethod",
+ "beforeMethod", "f", "afterMethod"
+ );
}
@Test
public void verifyDataProviderTrueFalse() {
- run(DataProviderTrueFalseTest.class, 1, 3);
+ List<String> invokedMethodNames = run(DataProviderTrueFalseTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f", "afterMethod",
+ "f", "afterMethod",
+ "f", "afterMethod"
+ );
}
@Test
public void verifyDataProviderFalseTrue() {
- run(DataProviderFalseTrueTest.class, 3, 1);
+ List<String> invokedMethodNames = run(DataProviderFalseTrueTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f",
+ "beforeMethod", "f",
+ "beforeMethod", "f", "afterMethod"
+ );
}
@Test
public void verifyDataProviderTrueTrue() {
- run(DataProviderTrueTrueTest.class, 1, 1);
+ List<String> invokedMethodNames = run(DataProviderTrueTrueTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f",
+ "f",
+ "f", "afterMethod"
+ );
}
@Test
public void verifyInvocationCountFalseFalse() {
- run(InvocationCountFalseFalseTest.class, 3, 3);
+ List<String> invokedMethodNames = run(InvocationCountFalseFalseTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f", "afterMethod",
+ "beforeMethod", "f", "afterMethod",
+ "beforeMethod", "f", "afterMethod"
+ );
}
@Test
public void verifyInvocationCountTrueFalse() {
- run(InvocationCountTrueFalseTest.class, 1, 3);
+ List<String> invokedMethodNames = run(InvocationCountTrueFalseTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f", "afterMethod",
+ "f", "afterMethod",
+ "f", "afterMethod"
+ );
}
@Test
public void verifyInvocationCountFalseTrue() {
- run(InvocationCountFalseTrueTest.class, 3, 1);
+ List<String> invokedMethodNames = run(InvocationCountFalseTrueTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f",
+ "beforeMethod", "f",
+ "beforeMethod", "f", "afterMethod"
+ );
}
@Test
public void verifyInvocationCountTrueTrue() {
- run(InvocationCountTrueTrueTest.class, 1, 1);
+ List<String> invokedMethodNames = run(InvocationCountTrueTrueTest.class);
+
+ assertThat(invokedMethodNames).containsExactly(
+ "beforeMethod", "f",
+ "f",
+ "f", "afterMethod"
+ );
}
- private void run(Class cls, int expectedBefore, int expectedAfter) {
- TestNG tng = new TestNG();
- tng.setVerbose(0);
- tng.setTestClasses(new Class[] { cls });
+ private static List<String> run(Class<?> cls) {
+ TestNG tng = create(cls);
+ InvokedMethodNameListener listener = new InvokedMethodNameListener();
+ tng.addListener(listener);
+
tng.run();
- try {
- Method before = cls.getMethod("getBeforeCount", new Class[0]);
- Integer beforeCount = (Integer) before.invoke(null, (Object[]) null);
- Assert.assertEquals(beforeCount.intValue(), expectedBefore);
-
- Method after = cls.getMethod("getAfterCount", new Class[0]);
- Integer afterCount = (Integer) after.invoke(null, (Object[]) null);
- Assert.assertEquals(afterCount.intValue(), expectedAfter);
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
+ return listener.getInvokedMethodNames();
}
}
diff --git a/src/test/java/test/invocationcount/InvocationBase.java b/src/test/java/test/invocationcount/InvocationBase.java
index bea3807..fe80bf8 100644
--- a/src/test/java/test/invocationcount/InvocationBase.java
+++ b/src/test/java/test/invocationcount/InvocationBase.java
@@ -2,7 +2,7 @@ package test.invocationcount;
import org.testng.annotations.Test;
-public class InvocationBase extends Base {
+public class InvocationBase {
@Test(invocationCount = 3)
public void f() {
}
diff --git a/src/test/java/test/invocationcount/InvocationCountFalseFalseTest.java b/src/test/java/test/invocationcount/InvocationCountFalseFalseTest.java
index 838eb92..e325f09 100644
--- a/src/test/java/test/invocationcount/InvocationCountFalseFalseTest.java
+++ b/src/test/java/test/invocationcount/InvocationCountFalseFalseTest.java
@@ -5,12 +5,8 @@ import org.testng.annotations.BeforeMethod;
public class InvocationCountFalseFalseTest extends InvocationBase {
@BeforeMethod(firstTimeOnly = false)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = false)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
diff --git a/src/test/java/test/invocationcount/InvocationCountFalseTrueTest.java b/src/test/java/test/invocationcount/InvocationCountFalseTrueTest.java
index 6d844df..7e5306b 100644
--- a/src/test/java/test/invocationcount/InvocationCountFalseTrueTest.java
+++ b/src/test/java/test/invocationcount/InvocationCountFalseTrueTest.java
@@ -5,12 +5,8 @@ import org.testng.annotations.BeforeMethod;
public class InvocationCountFalseTrueTest extends InvocationBase {
@BeforeMethod(firstTimeOnly = false)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = true)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
diff --git a/src/test/java/test/invocationcount/InvocationCountTrueFalseTest.java b/src/test/java/test/invocationcount/InvocationCountTrueFalseTest.java
index a1d9af7..06a5bbb 100644
--- a/src/test/java/test/invocationcount/InvocationCountTrueFalseTest.java
+++ b/src/test/java/test/invocationcount/InvocationCountTrueFalseTest.java
@@ -5,12 +5,8 @@ import org.testng.annotations.BeforeMethod;
public class InvocationCountTrueFalseTest extends InvocationBase {
@BeforeMethod(firstTimeOnly = true)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = false)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
diff --git a/src/test/java/test/invocationcount/InvocationCountTrueTrueTest.java b/src/test/java/test/invocationcount/InvocationCountTrueTrueTest.java
index 0a2676f..f2c1d5c 100644
--- a/src/test/java/test/invocationcount/InvocationCountTrueTrueTest.java
+++ b/src/test/java/test/invocationcount/InvocationCountTrueTrueTest.java
@@ -5,12 +5,8 @@ import org.testng.annotations.BeforeMethod;
public class InvocationCountTrueTrueTest extends InvocationBase {
@BeforeMethod(firstTimeOnly = true)
- public void beforeMethod() {
- incrementBefore();
- }
+ public void beforeMethod() {}
@AfterMethod(lastTimeOnly = true)
- public void afterMethod() {
- incrementAfter();
- }
+ public void afterMethod() {}
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git
More information about the pkg-java-commits
mailing list