Skip to content

Commit

Permalink
Fixed: onTestSuccess() was called after @AfterMethod instead of after…
Browse files Browse the repository at this point in the history
… the test method (test: test.listener.ListenerTest)
  • Loading branch information
cbeust committed Jun 27, 2010
1 parent 37d27f3 commit 721fb91
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -3,6 +3,7 @@ Current:
Added: New ant task tag: propertyset (Todd Wells)
Added: ITestNGListenerFactory
Added: Passing command line properties via the ant task and doc update (Todd Wells)
Fixed: onTestSuccess() was called after @AfterMethod instead of after the test method (test: test.listener.ListenerTest)
Fixed: XML test results contained skipfailedinvocationCounts instead of skipfailedinvocationcounts
Fixed: Issue4 assertEquals for primitive arrays, Issue34 assertNull javadoc updated
Fixed: Issue78 NPE with non-public class. Now throws TestNG exception
Expand Down
10 changes: 7 additions & 3 deletions src/org/testng/internal/Invoker.java
Expand Up @@ -631,6 +631,10 @@ private ITestResult invokeMethod(Object[] instances,
tm.addFailedInvocationNumber(parametersIndex);
}

//@@
if (testResult.getStatus() == ITestResult.SUCCESS) {
runTestListeners(testResult);
}
//
// Increment the invocation count for this method
//
Expand Down Expand Up @@ -1005,7 +1009,6 @@ public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,
int parametersIndex = 0;

try {
// if (true) { // @@
List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();

// if (false) { // bag.parameterHolder.origin == ParameterHolder.ORIGIN_DATA_PROVIDER) {
Expand Down Expand Up @@ -1341,8 +1344,9 @@ else if(ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {
else {
assert false : "UNKNOWN STATUS:" + status;
}

if (triggerListeners) {
//@@
// if (triggerListeners) {
if (triggerListeners && status != ITestResult.SUCCESS) {
runTestListeners(testResult);
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/src/test/listeners/ListenerTest.java
@@ -0,0 +1,23 @@
package test.listeners;

import java.util.Arrays;

import junit.framework.Assert;

import org.testng.TestNG;
import org.testng.annotations.Test;

import test.SimpleBaseTest;

public class ListenerTest extends SimpleBaseTest {

/**
* Ensure that if a listener is present, we get test(), onSuccess(), afterMethod()
*/
@Test
public void listenerShouldBeCalledBeforeConfiguration() {
TestNG tng = create(OrderedListenerSampleTest.class);
tng.run();
Assert.assertEquals(Arrays.asList(1, 2, 3, 4), SimpleListener.m_list);
}
}
35 changes: 35 additions & 0 deletions test/src/test/listeners/OrderedListenerSampleTest.java
@@ -0,0 +1,35 @@
package test.listeners;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.collections.Lists;

import test.SimpleBaseTest;

@Listeners(SimpleListener.class)
public class OrderedListenerSampleTest extends SimpleBaseTest {

@BeforeClass
public void init() {
SimpleListener.m_list = Lists.newArrayList();
}

@BeforeMethod
public void bm() {
SimpleListener.m_list.add(1);
}

@Test
public void f() {
SimpleListener.m_list.add(2);
}

@AfterMethod
public void am() {
SimpleListener.m_list.add(4);
}
}

16 changes: 16 additions & 0 deletions test/src/test/listeners/SimpleListener.java
@@ -0,0 +1,16 @@
package test.listeners;

import java.util.List;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class SimpleListener extends TestListenerAdapter {
public static List<Integer> m_list;

public void onTestSuccess(ITestResult tr) {
SimpleListener.m_list.add(3);
super.onTestSuccess(tr);
}

}

0 comments on commit 721fb91

Please sign in to comment.