Announcement Announcement Module
Collapse
No announcement yet.
unittest advice class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • unittest advice class

    We are using "advice" to our service classes to provide logging of exceptions. (My team believes it is the responsibillity of the service layer to log its exceptions) So I wanted to write a unit test that confirms that all classes in the spring context have a "postadvice" related to logging runtime exceptions.

    This code works, but seems a little fragile. Any better way of doing it?
    Since this is CGLIB magic... any better way to get a handle to the Advise?

    tx
    Code:
    private void assertPubMethodsHaveAdvice(String beanName) throws NoSuchFieldException, IllegalAccessException
        {
            Object service = applicationContext.getBean(beanName);
            
            Method[] methods = service.getClass().getMethods();
            for &#40;int i=0;i<methods.length; i++&#41;
            &#123;
                Method meth = methods&#91;i&#93;;
                if &#40;meth.getDeclaringClass&#40;&#41;.equals&#40;service.getClass&#40;&#41;&#41;&#41; //don't grab methods in parent class
                &#123;
                    Object cglib = &#40;Object&#41;PrivilegedAccessor.getValue&#40;service, "CGLIB$CALLBACK_4"&#41;;
                    Object cglib2 = &#40;Object&#41;    PrivilegedAccessor.getValue&#40;cglib, "this$0"&#41;;
                    Object cglib3 = &#40;Object&#41;    PrivilegedAccessor.getValue&#40;cglib2, "advised"&#41;;
                    Advisor&#91;&#93; advisors = &#40;Advisor&#91;&#93;&#41;    PrivilegedAccessor.getValue&#40;cglib3, "advisorArray"&#41;;
                    Advisor advisor = advisors&#91;1&#93;;
                    DataAccessExceptionAdvice advice = &#40;DataAccessExceptionAdvice&#41;advisor.getAdvice&#40;&#41;;
                    assertNotNull&#40;"bean has no advice!"+beanName, advice&#41;;
                    
                &#125;
            &#125;
        &#125;

  • #2
    fragile isn't the word

    Not sure how else you could do it though. Not sure I would test it, but I probably should

    Comment


    • #3
      If I were going to do this I would:
      - make the logger pluggable in the advise class (probably use commons logging)
      - make a mock logger that acts like Dumbster (the fake SMTP server used for unit testing)
      - in my unit test, plug in the mock logger.
      - then after I call a method where I want to see if logging happened, get the mock logger and
      query it to see it the logging happened (just like Dumpster).

      Comment


      • #4
        Use org.subethamail.wiser.Wiser in preference to Dumbster

        Originally posted by Will Jaynes View Post
        If I were going to do this I would:
        - make the logger pluggable in the advise class (probably use commons logging)
        - make a mock logger that acts like Dumbster (the fake SMTP server used for unit testing)
        - in my unit test, plug in the mock logger.
        - then after I call a method where I want to see if logging happened, get the mock logger and
        query it to see it the logging happened (just like Dumpster).
        I went through a lot of pain using Dumbster until I discovered a Dumbster replacement written by someone who got frustrated with the bugs in Dumbster.
        Below is the comment I placed in the program I wrote that originally used Dumbster:

        * The first version of this program used the open source Dumbster mock mail server.
        * However a robust, well-written open source alternative was later found and the program was re-written to use org.subethamail.wiser.Wiser instead of Dumbster.
        * Wiser, according to their web site, is quote, "a smart replacement for Dumbster".
        * For more information see: http://subethasmtp.tigris.org/wiser.html

        Comment

        Working...
        X