log4j unit test failure + sonar

View: New views
5 Messages — Rating Filter:   Alert me  

log4j unit test failure + sonar

by harshavsn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a unit test which verifies that certain messages are printed to the logger.  Does sonar have some logic which changes the behaviour of the logger?

For more detail here is the code:
        // setup unit test
        String caller = "test caller";
        String cdiMessage = "test log message";
        String logMessage = "- CDI api log - Caller: "+caller+" - Message: "+cdiMessage+Layout.LINE_SEP;
        StringWriter writer = new StringWriter();
        SimpleLayout layout = new SimpleLayout();
        BasicConfigurator.configure(new WriterAppender(layout, writer));
        org.apache.log4j.LogManager.getLoggerRepository().setThreshold(org.apache.log4j.Level.INFO);

        // write message to log
        mClient.onMsgLog(new CDIMsgLogInfo(caller, infoLevel, cdiMessage));

        // verify message was written
        assertEquals("INFO "+logMessage, writer.toString());


And the error is:
org.junit.ComparisonFailure: expected:<[INFO - CDI api log - Caller: test caller - Message: test log message
]> but was:<[]>

However when run without sonar the test passes.  




Re: log4j unit test failure + sonar

by Freddy Mallet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Harsha,

What happens when you're launching "mvn test" ? I guess the same unexpected behavior happens as there might be a conflict between the Maven logger configuration and your application logger configuration ?

Regards,
Freddy


On Wed, Oct 21, 2009 at 7:10 PM, harshavsn <harsha.sri-narayana@...> wrote:

I have a unit test which verifies that certain messages are printed to the
logger.  Does sonar have some logic which changes the behaviour of the
logger?

For more detail here is the code:
       // setup unit test
       String caller = "test caller";
       String cdiMessage = "test log message";
       String logMessage = "- CDI api log - Caller: "+caller+" - Message:
"+cdiMessage+Layout.LINE_SEP;
       StringWriter writer = new StringWriter();
       SimpleLayout layout = new SimpleLayout();
       BasicConfigurator.configure(new WriterAppender(layout, writer));

org.apache.log4j.LogManager.getLoggerRepository().setThreshold(org.apache.log4j.Level.INFO);

       // write message to log
       mClient.onMsgLog(new CDIMsgLogInfo(caller, infoLevel, cdiMessage));

       // verify message was written
       assertEquals("INFO "+logMessage, writer.toString());


And the error is:
org.junit.ComparisonFailure: expected:<[INFO - CDI api log - Caller: test
caller - Message: test log message
]> but was:<[]>

However when run without sonar the test passes.




--
View this message in context: http://www.nabble.com/log4j-unit-test-failure-%2B-sonar-tp25996519p25996519.html
Sent from the Sonar user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: log4j unit test failure + sonar

by harshavsn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Freddy,

"mvn test" works as does "mvn install"
However I can see the test log messages in the maven log output when I run these commands so it is writing to the main logger.

regards,
Harsha

Freddy Mallet wrote:
What happens when you're launching "mvn test" ? I guess the same unexpected
behavior happens as there might be a conflict between the Maven logger
configuration and your application logger configuration ?

Re: log4j unit test failure + sonar

by harshavsn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Should I file a bug report on this?

Is there a way to ignore a single test in sonar?  

regards
Harsha

Re: log4j unit test failure + sonar

by Simon Brandhof-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Harsha,

Should I file a bug report on this?

Yes please. I'm afraid that there's a conflict with SLF4J/logback used by the Sonar maven plugin.
 

Is there a way to ignore a single test in sonar?

No. The only workaround is to deactivate execution of unit tests and to reuse XML reports generated by the install phase :
mvn sonar:sonar -Dsonar.dynamicAnalysis=reuseReports
See also sonar.surefire.reportsPath and sonar.cobertura.reportPath if you don't use standard paths.

Regards,
Simon