logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: Problem mocking log4net appender (Repost)
Date Sun, 17 Dec 2006 21:49:58 GMT
log4net.Tests.Appender.CountingAppender is a more complete implementation of my example.

----- Original Message ----
From: Ron Grabowski <rongrabowski@yahoo.com>
To: Log4NET User <log4net-user@logging.apache.org>
Sent: Sunday, December 17, 2006 1:25:57 PM
Subject: Re: Problem mocking log4net appender (Repost)

Have you tried your test case using RhinoMocks?

http://www.ayende.com/projects/rhino-mocks.aspx

The IAppender interface is only one method and two properties. Can you bypass using a mock
object framework and just implement a class for testing? Aren't mocks more useful for non-trivial
interfaces?

// untested
public class DoAppendCallCountAppender : IAppender
{
 private int doAppenderCallCount = 0;
 
 public void Close()
 {
  /* empty */
 }

 public void DoAppend(LoggingEvent loggingEvent)
 {
  doAppenderCallCount++;
 }

 public string Name
 {
  get { return "DoAppendNeverCalledAppender"; }
  set { /* empty */ }
 }

 public int DoAppenderCallCount

 {

  get { return doAppenderCallCount; }

 }
}

----- Original Message ----
From: Matt Young <matt.young256@gmail.com>
To: log4net-user@logging.apache.org; Matthew Young <matt.young256@gmail.com>
Sent: Tuesday, December 5, 2006 12:22:40 PM
Subject: Problem mocking log4net appender (Repost)

Greetings,

Can anyone suggest a workaround to this problem?

I attempting to mock a log4net (1.2.10.0) appender with NMock2
(1.0.2313.18049). The mock appender throws an ExpectionException when
log4net calls DoAppend and then log4net eats the exception the test
finishes successfully. Unfortunately
VerifyAllExpectationsHaveBeenMet() does not cause the test to fail.
Apparently the mock appender does not remember that the Expect.Never
expectation was broken.

Thanks :)

Here's the code,
--------------------------------------------------------------
using System;
using NUnit.Framework;
using NMock2;

namespace LoggerTest
{
   public class Worker
   {
       private static readonly log4net.ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
       public void DoWork()
       {
           logger.Error("oops.");
       }
   }

   [TestFixture]
   public class Tester
   {
       private Mockery mockery;
       private log4net.Appender.IAppender appender;

       [SetUp] public void SetUp()
       {
           mockery = new Mockery();
           appender = mockery.NewMock<log4net.Appender.IAppender>();

           log4net.Config.BasicConfigurator.Configure(appender);
           log4net.Repository.Hierarchy.Hierarchy rep =
(log4net.Repository.Hierarchy.Hierarchy)
log4net.LogManager.GetRepository();
           rep.Threshold = log4net.Core.Level.Info;

           Expect.Never.On(appender).Method("DoAppend").WithAnyArguments();
       }

       [TearDown] public void TearDown()
       {
           log4net.Repository.Hierarchy.Hierarchy rep =
(log4net.Repository.Hierarchy.Hierarchy)
log4net.LogManager.GetRepository();
           rep.Root.RemoveAppender(appender);
           rep.Configured = false;
       }

       [Test] public void Test1()
       {
           Worker worker = new Worker();
           worker.DoWork();
           mockery.VerifyAllExpectationsHaveBeenMet();
       }
   }
}

---------------------------------------------------
------ Test started: Assembly: LoggerTest.dll ------

Error: log4net:ERROR Log: Exception while logging
log4net:ERROR Log: Exception while logging
Error: NMock2.Internal.ExpectationException: unexpected invocation of
appender.Name
Expected:

  at NMock2.Mockery.FailUnexpectedInvocation(Invocation invocation)
  at NMock2.Mockery.Dispatch(Invocation invocation)
  at NMock2.Mockery.MockObject.Invoke(Invocation invocation)
  at NMock2.Monitoring.Invoker.Invoke(Invocation invocation)
  at NMock2.Monitoring.ProxiedObjectIdentity.Invoke(Invocation invocation)
  at NMock2.Monitoring.ProxyInvokableAdapter.Invoke(IMessage msg)
  at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type)
  at log4net.Appender.IAppender.get_Name()
  at log4net.Util.AppenderAttachedImpl.AppendLoopOnAppenders(LoggingEvent
loggingEvent)
  at log4net.Repository.Hierarchy.Logger.CallAppenders(LoggingEvent
loggingEvent)
  at log4net.Repository.Hierarchy.Logger.ForcedLog(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)
  at log4net.Repository.Hierarchy.Logger.Log(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)
NMock2.Internal.ExpectationException: unexpected invocation of appender.Name
Expected:

  at NMock2.Mockery.FailUnexpectedInvocation(Invocation invocation)
  at NMock2.Mockery.Dispatch(Invocation invocation)
  at NMock2.Mockery.MockObject.Invoke(Invocation invocation)
  at NMock2.Monitoring.Invoker.Invoke(Invocation invocation)
  at NMock2.Monitoring.ProxiedObjectIdentity.Invoke(Invocation invocation)
  at NMock2.Monitoring.ProxyInvokableAdapter.Invoke(IMessage msg)
  at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type)
  at log4net.Appender.IAppender.get_Name()
  at log4net.Util.AppenderAttachedImpl.AppendLoopOnAppenders(LoggingEvent
loggingEvent)
  at log4net.Repository.Hierarchy.Logger.CallAppenders(LoggingEvent
loggingEvent)
  at log4net.Repository.Hierarchy.Logger.ForcedLog(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)
  at log4net.Repository.Hierarchy.Logger.Log(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)

1 passed, 0 failed, 0 skipped, took 0.98 seconds.

--
Matt







Mime
View raw message