You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Maher Martin <MM...@Webasto.de> on 2003/02/17 15:59:51 UTC

[Logging] Error using logging with Unit Tests

Hi,

I'm having problems with the logging component in conjunction with Unit
Tests (JUnit). Within a testCase I cannot use the logging component. I keep
getting the error:

Class org.apache.commons.logging.impl.Log4JCategoryLog does not implement
Log

I looked at the class Log4JCategoryLog and it clearly does implement the
class Log.

Below you can find the full stack trace as well as a sample Unit Test Class.

Note: 
- The Main method within the test class does not throw any exception,
however the testCase does.
- I have not explicitly specified which Log Implementation to use, however
Log4J is available within the classpath.

thanks,

Martin Maher

##############
Exception:

0 [main] INFO part.TestLogger  - Test 1 

org.apache.commons.logging.LogConfigurationException: 
org.apache.commons.logging.LogConfigurationException: 
org.apache.commons.logging.LogConfigurationException: Class
org.apache.commons.logging.impl.Log4JCategoryLog does not implement Log

at
org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.ja
va:555) 	
at
org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.ja
va:289) 	
at
org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.ja
va:259) 	
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:390) 	
at
com.webasto.cimload.transformer.part.TestLogger.outputLogMessage(TestLogger.
java:44) 	
at com.webasto.cimload.transformer.part.TestLogger.testA(TestLogger.java:36)

at java.lang.reflect.Method.invoke(Native Method) 	
at junit.framework.TestCase.runTest(TestCase.java:166) 	
at junit.framework.TestCase.runBare(TestCase.java:140) 	
at junit.framework.TestResult$1.protect(TestResult.java:106) 	
at junit.framework.TestResult.runProtected(TestResult.java:124) 	
at junit.framework.TestResult.run(TestResult.java:109) 	
at junit.framework.TestCase.run(TestCase.java:131) 	
at junit.framework.TestSuite.runTest(TestSuite.java:173) 	
at junit.framework.TestSuite.run(TestSuite.java:168) 	
at junit.swingui.TestRunner$17.run(TestRunner.java:644) 

##############
Class:

package com.webasto.cimload.transformer.part;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import junit.framework.TestCase;
import junit.framework.TestSuite;

public class TestLogger extends TestCase {

  public TestLogger(String _name) {
    super(_name);
  }

  public void testA() {
    try {
      outputLogMessage("Test 2");
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public static void outputLogMessage(String msg) {
    Log log = LogFactory.getLog(TestLogger.class);
    log.info(msg);
  }

  public static void main(String[] argv) {

    outputLogMessage("Test 1");

    String[] testCaseList = {TestLogger.class.getName()};

    junit.swingui.TestRunner.main(testCaseList);
  }

}

Re: [Logging] Error using logging with Unit Tests

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Mon, 17 Feb 2003, Maher Martin wrote:

> Date: Mon, 17 Feb 2003 15:59:51 +0100
> From: Maher Martin <MM...@Webasto.de>
> Reply-To: Jakarta Commons Users List <co...@jakarta.apache.org>
> To: 'Jakarta Commons Users List' <co...@jakarta.apache.org>
> Subject: [Logging] Error using logging with Unit Tests
>
> Hi,
>
> I'm having problems with the logging component in conjunction with Unit
> Tests (JUnit). Within a testCase I cannot use the logging component. I keep
> getting the error:
>
> Class org.apache.commons.logging.impl.Log4JCategoryLog does not implement
> Log
>
> I looked at the class Log4JCategoryLog and it clearly does implement the
> class Log.
>
> Below you can find the full stack trace as well as a sample Unit Test Class.
>
> Note:
> - The Main method within the test class does not throw any exception,
> however the testCase does.
> - I have not explicitly specified which Log Implementation to use, however
> Log4J is available within the classpath.
>

This sounds like a case of having commons-logging in multiple class
loaders at the same time -- if the Log interface is loaded from class
loader A, but the Log4JCategoryLog class is loaded from class loader B
that is a parent of A, then Log4JCategoryLog will load B's copy of the Log
interface -- and the JVM does not consider the two log interfaces to be
assignment-compatible or castable (even though the signatures might all be
identical.)

If you run your JUnit tests from Ant, I have found it most effective to
use fork="yes" so that everything runs in a separate JVM, where you can
set up precisely the class path you need (instead of having it run in the
same class path that Ant is using).  Successful examples of doing this
(including the use of logging in the test code) can be found in most of
the commons package source distributions - the ones' I'm most familiar
with are for beanutils and digester.

> thanks,
>
> Martin Maher
>

Craig