You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Thomas SMETS <ts...@altern.org> on 2001/12/02 14:31:52 UTC

Using Log 4 J statementin JUnit

Hi,

I'm using Junit to functionnaly test my classes & composant behavior.
I'm willing in the TestCase s to have some logging statements via Log4J.

Classes have the typical structure : see @ the bottom.

The TestAll class simply adds in its TestSuite, all the tests from the 
classes in the package but the main of TestAll.java containts the 
following line :
<pre>
   public static void main(String[] args)
   {
     PropertyConfigurator.configure (PROPERTY_FILE_NAME);
     junit.swingui.TestRunner.run (TestAll.class);
   }
</pre>

Unfortunately the Tested classes cannot use the Appender  defined in the 
Property-file. Could someone give me a nice & elegant way to have the 
Log4J working with JUnit tests ?

Tx,

Thomas,



<pre>
package suncertify.enhenceddb.test;

import org.apache.log4j.*;
import junit.framework.*;

// Import of the class to test only
import suncertify.enhenceddb.DatabaseException;

/**
   *
   */
public class TestDatabaseException
   extends TestCase
{

   private static Logger log = Logger.getLogger 
(TestDatabaseException.class);

   private DatabaseException DBe = null;

   public TestDatabaseException(String aTestName)
   {
     super(aTestName);
   }


/**
   * Preparation of the <code>TestCase</code>.
   */
   public void setUp()
   {
     return;
   }

/**
   *
   */
   public void createDatabaseException()
   {
     DBe = new DatabaseException ();
     assertNotNull ( "Could not create an Exception of type 
DatabaseException",
                     DBe);
   }


/**
   *
   */
   public void createAndThrowDatabaseException()
   {
     try
     {
       DBe = new DatabaseException ();
       throw DBe;

     } catch(DatabaseException DBEe)
     {
       assertNotNull(  "Exception of type DatabaseException thrown but 
apparently null",
                       DBEe);
     }
   }

/**
   *
   */
   public static TestSuite suite()
   {
     TestSuite suite = new TestSuite();
       log.info ("TestSuite created");
       suite.addTest (new TestDatabaseException 
("createDatabaseException"));
       log.info ("createDatabaseException");
       suite.addTest (new TestDatabaseException 
("createAndThrowDatabaseException"));
       log.info ("createAndThrowDatabaseException");
     return suite;
   }

/**
   * Dereferencing of the <code>TestCase</code>.
   */
   public void tearDown()
   {
     DBe = null;
     return;
   }


/**
   * This main method simply calls upon the TestCase.
   *
   * @param args The classical String-array a main method accepts.
   * @return -
   */
   public static void main(String[] args)
   {
     BasicConfigurator.configure ();
     junit.swingui.TestRunner.run (TestDatabaseException.class);
   }
}

</pre>




-- 


rue J. Wytsman 62
B - 1050 Brussels
Belgium
Tel. : +32 (0)2 742 05 94 (GMT+1)
yahoo-id : smetsthomas



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>