I think I have another workaround. [It is kinda related to Mark Hansen's approach in that it uses another class, but the class is used differently here (I think). I only make a call once, instead of each time in setup().]

Here it is: If inside a test case method in my TestCase class I instantiate another Java object that has a logger, the rest of my test cases get their logs sent to the JUnit Reports correctly.

That is, suppose I have a regular class Outsider.java, which has its own configured logger. Then, I have a TestCase SomeTest.java, which also has a logger.

Say SomeTest.java has 3 test cases:

        public void testBefore() {
                logger.debug("logger.debug - testBefore");
                System.out.println("System.out.println - testBefore");
        }

        public void testExternalCall() {
                logger.debug("logger.debug - testExternalCall");
                Outsider o = new Outsider();
                logger.debug("logger.debug - just instantiated Outsider");
        }

        public void testAfter() {
                logger.debug("logger.debug - testAfter");
                System.out.println("System.out.println - testAfter");
        }

As we have been discussing, sometimes the logger statements do not make it to the final Junit Reports. Only the System.out.println() statements make it. This is true for testBefore() and the first logger statement of testExternalCall().

However, after the instantiation:
Outsider o = new Outsider();
both logger and System.out.println() statements make it to the final report.

Even later test cases, like testAfter, that don't have this instantiation work correctly.

So the above was just to show the "before" and "after" effects of the instantiation. The full workaround, then, would just be to make sure the top-most test case of each Test.java is like this:

        public void testFirstSetupLogger() {
                Outsider o = new Outsider();
        }
        ... rest of your test cases go here ...

Then, all the rest of the test cases that follow will work correctly! :)

Several questions:

1) This workaround seems to work. But is there any drawback that you can see?

2) I suppose the reason for this behavior has something to do with what others were talking about earlier regarding the miscommunication between appenders or something. But I didn't get that exactly. Could someone help me further understand why this behavior happens?

3) Is this problem a bug?  If so, who would own it - junit? ant? or log4j?

I have attached the files below, to clarify what I'm talking about.

Thanks,
Oski

Versions:
Ant 1.6.2
JUnit 3.8.1
Log4j 1.2.9

Here are the four files:
(1) /src/Outsider.java
(2) /src/SomeTest.java
(3) /src/log.properties
(4) /build.xml

---------------------------------------
(1) /src/Outsider.java
---------------------------------------
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Outsider {

        Logger logger;

        public Outsider() {
                logger = Logger.getLogger(Outsider.class);
                PropertyConfigurator.configure("log.properties");
        }

}

---------------------------------------
(2) /src/SomeTest.java
---------------------------------------
import junit.framework.TestCase;

import org.apache.log4j.Logger;

public class SomeTest extends TestCase {

        Logger logger;

        public SomeTest(String name) {
                super(name);
                logger = Logger.getLogger(SomeTest.class);
        }

        public void testSetupLog() {
                //Uncomment the line below and run again to see the workaround
                //Outsider o = new Outsider();
        }

        public void testBefore() {
                logger.debug("logger.debug - testBefore"); // NO, not in report
                System.out.println("System.out.println - testBefore"); // YES, 
in report
        }

public void testExternalCall() {
logger.debug("logger.debug - testExternalCall"); // NO
Outsider o = new Outsider();
logger.debug("logger.debug - just instantiated Outsider"); // YES! it works
}


        public void testAfter() {
                logger.debug("logger.debug - testAfter"); // YES! it works
                System.out.println("System.out.println - testAfter"); // YES
        }

}

---------------------------------------
(3) /src/log.properties
---------------------------------------
log4j.rootLogger=DEBUG, A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] (%F:%L) - %m%n

---------------------------------------
(4) /build.xml
---------------------------------------
<project name="tempTestLog4jAndJUnitInAnt" default="build" basedir=".">

        <property name="src.dir" value="${basedir}/src" />
        <property name="build.dir" value="${basedir}/build" />
        <property name="lib.dir" value="${basedir}/lib" />
        <property name="log.dir" value="${basedir}/log" />

        <!-- Construct Classpath -->
        <path id="classpath">
                <pathelement location="${build.dir}" />
                <pathelement location="lib/junit-3.8.1/junit.jar" />
                <pathelement location="lib/log4j-1.2.9/log4j-1.2.9.jar" />
        </path>

<target name="build" description="compile all Java files and put class files and other files in build">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="classpath" />
</javac>
</target>


<target name="test" depends="build" description="run build verification and regression tests (build/**/*Test*), capture results in XML and HTML logs.
Note: Add junit.jar to ${ANT_HOME}/lib first.">
<!-- make needed subdirs -->
<delete dir="${log.dir}" failonerror="false" />
<mkdir dir="${log.dir}" />
<mkdir dir="${log.dir}/xml" />
<mkdir dir="${log.dir}/html" />


<!-- do tests and generate XML results -->
<junit printsummary="yes" fork="yes" dir="${build.dir}" haltonfailure="no">
<classpath refid="classpath" />
<formatter type="xml" />
<batchtest todir="${log.dir}/xml">
<fileset dir="${build.dir}">
<include name="**/*Test*" />
</fileset>
</batchtest>
</junit>


                <!-- generate HTML report -->
                <junitreport todir="${log.dir}/xml">
                        <fileset dir="${log.dir}/xml">
                                <include name="TEST-*.xml" />
                        </fileset>
                        <report format="frames" todir="${log.dir}/html" />
                </junitreport>
        </target>

</project>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to