History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEADEV-14731
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Normal Normal
Assignee: Alexey Kudravtsev
Reporter: Gennadii Donchyts
Votes: 2
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
IDEA: Development

JUnit custom RunListener not called when using custom runner in @RunWith(MyRunner.class)

Created: 01 Feb 07 21:09   Updated: 16 Apr 08 17:35
Component/s: Unit Testing. JUnit
Fix Version/s: Diana 8375, Selena 7.0.4

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown
Environment: JUnit 4.1, JUnit 4.2

Build: 6,148
Fixed in build: 8,300
Severity: High


 Description  « Hide
I'd like to setup custom output log file for each test in a separate file, to do this I've to detect test name each time tests are called.

My cusom runner:

package my.package;

import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.TestClassRunner;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;

public class MyRunner extends TestClassRunner  {
    private static class MyListener extends RunListener {
        @Override
        public void testStarted(Description description) throws Exception {  // <<<<<<<<<<< this method not called
            System.err.println(" STARTED: " + description.getDisplayName());
        }

        @Override
        public void testFinished(Description description) throws Exception {
            System.err.println("FINISHED: " + description.getDisplayName());
        }
    }

    public synchronized void run(final RunNotifier notifier) {
        notifier.addListener(new MyListener()); // subscribe listener
        super.run(notifier);
    }

    public MyRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    public MyRunner(Class<?> klass, Runner runner) throws InitializationError {
        super(klass, runner);
    }
}

My test:

package my.package;

import org.junit.*;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import org.apache.log4j.Logger;

import java.io.File;

@RunWith(MyRunner.class)
public class MyTests {
    private Logger log = Logger.getLogger(MyTests.class);

    @Before
    public void setup()
    {
    }
    /**
     * Logging for each separate test should go into test.log file located in the test output folder  
     */
    @Test
    public void logging() {
        log.debug("test log message");
    }
}

JUnit 4.2 TestMethodRunner.java:

public void run() {
		if (fTestIntrospector.isIgnored(fMethod)) {
			fNotifier.fireTestIgnored(fDescription);
			return;
		}
		fNotifier.fireTestStarted(fDescription); // <<<<<< this method does not work, probably source code is incorrect, can not debug inside
		try {
			long timeout= fTestIntrospector.getTimeout(fMethod);
			if (timeout > 0)
				runWithTimeout(timeout);
			else
				runMethod();
		} finally {
			fNotifier.fireTestFinished(fDescription);
		}
	}

When I debug JUnit in the method above stack trace shows that fNotifier is com.intellij.rt.junit4.Junit4TestMethodAdapter. Most probably it does not call super.fireXxx() because I can call listeners manually.



 All   Comments   Work Log   Change History      Sort Order:
Gennadii Donchyts - 21 Jun 07 18:24
Comment about JUnit support in IntelliJ IDEA on Java.net http://weblogs.java.net/blog/fabianocruz/archive/2006/06/junit_4_you_1.html