David Williams
David Williams

Reputation: 8654

How to Log to Console During Ant Testing Task

I have an ant task to run unit tests. It looks like this

<target name="test-platform" depends="compile_tests">
    <mkdir dir="${platform.test.log.dir}"/>
    <junit fork="true" haltonerror="true">
        <classpath refid="test.build.classpath"/>
        <batchtest todir="${platform.test.log.dir}">
            <formatter type="plain" usefile="true"/>
            <fileset dir="${javatests}">
                <include name="**/*Test.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

I have some test that looks like this:

public class UserTest  extends BaseTestClass {

    private static final Logger LOGGER = Logger.getLogger(BaseTestClass.class);

    @Autowired
    SessionFactory sessionFactory;

    @Test
    @SuppressWarnings("unchecked")
    public void testUser() {
        List<User> users = sessionFactory
            .getCurrentSession()
            .createQuery("from User")
            .list();
        for(User user : users) {
            LOGGER.info(user.getName());
            Assert.assertEquals(user.getName(),String.class);
        }
    }
}

I had created this BaseTestClass to contain a log4j configuration follow several other SO posts.

package com.example.test;
import org.apache.log4j.BasicConfigurator;
public class BaseTestClass {
    static {
        BasicConfigurator.configure();
    }
}

But when I run my tests, nothing comes out in the console. How do I fix this?

Upvotes: 2

Views: 2351

Answers (3)

Anu Shibin Joseph Raj
Anu Shibin Joseph Raj

Reputation: 1047

An even quicker way:

<junit showoutput="true">
<formatter type="plain" usefile="false"/>
<batchtest>
    <fileset dir="${src.tests}"></fileset>
</batchtest>

The usefile attribute is the game-changer here.

Reference from the Apache Ant manual:

... Output will always be sent to a file, unless you set the usefile attribute to false.

Upvotes: 0

user3139488
user3139488

Reputation:

Per : http://ant.apache.org/manual/Tasks/junit.html , try with setting showoutput="true" and log4j reference can be explicitly passed with <jvmarg value="-Dlog4j.configuration=file>

Upvotes: 3

Simon
Simon

Reputation: 536

A quick way to get up and running:

Create a log4j.properties file and put it in the root of your class path.

enter the following lines. The pattern you use is up to you.

log4j.rootLogger = INFO, Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.conversionPattern=%d [%t] %5p %m%n

You may need to remove BaseTestClass.

Upvotes: 0

Related Questions