1 2 Previous Next 25 Replies Latest reply: Oct 23, 2008 10:31 AM by 807589 RSS

    Handlers in JUnit tests?

    807589
      Hi,

      Is there any easy way to set handlers in JUnit tests to detect tests that are failing (for example) and catch the corresponding event or should I implement this myself?

      Thanks,

      J.
        • 1. Re: Handlers in JUnit tests?
          800282
          You can do that like this:
          @Test(expected=AnException.class)
          public void testAnException() {
              // do something that _should_ throw 'AnException'
          }
          • 2. Re: Handlers in JUnit tests?
            807589
            Thank you for your suggestion, but I am looking for something like setListener(...).

            J.
            • 3. Re: Handlers in JUnit tests?
              807589
              Jrm wrote:
              Thank you for your suggestion, but I am looking for something like setListener(...).
              Why? What are you trying to do? You've described how you're trying to do it, but what is the overall problem you are trying to solve?

              ~
              • 4. Re: Handlers in JUnit tests?
                807589
                When you run JUnit tests, you will get messages if they fail (to make it simple). However, I want to be able to run statistics. Therefore, I need to capture the total number of tests and the set of tests which failed (knowing that the set of tests changes during a project). I was thinking about overriding the assertion methods, but I was wondering whether another solution was already available (I searched but could not find anything).

                Thanks,

                J.
                • 5. Re: Handlers in JUnit tests?
                  807589
                  Jrm wrote:
                  When you run JUnit tests, you will get messages if they fail (to make it simple). However, I want to be able to run statistics. Therefore, I need to capture the total number of tests and the set of tests which failed (knowing that the set of tests changes during a project). I was thinking about overriding the assertion methods, but I was wondering whether another solution was already available (I searched but could not find anything).
                  Have you considered the [<junitreport>|http://ant.apache.org/manual/OptionalTasks/junitreport.html] task?

                  ~
                  • 6. Re: Handlers in JUnit tests?
                    807589
                    Seems like RunListener is what you are looking for.
                    [http://junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html]

                    - Roy
                    • 7. Re: Handlers in JUnit tests?
                      807589
                      Bingo Roy, that's what I was looking for !!! Many thanks !!!

                      J.
                      • 8. Re: Handlers in JUnit tests?
                        807589
                        Jrm wrote:
                        Bingo Roy, that's what I was looking for !!! Many thanks !!!
                        Bet it isn't, really. All common JUnit runners already generate these statistics for you, and more complex runners such as Ant or Maven's JUnit runners will generate nice neat reports for you, or even a whole website detailing what passed and what failed, with summaries, links to more details and other goodies. You're better off learning how to use tools provided, than hacking about trying to badly re-invent a perfectly good set of wheels
                        • 9. Re: Handlers in JUnit tests?
                          807589
                          georgemc wrote:
                          Bet it isn't, really.
                          I agree, and it's why I suggested just adding a <junitreport> to an Ant target. But the OP didn't seem receptive to that suggestion and seems dead-set on reinventing the wheel. Perhaps there's a good reason that hasn't yet been communicated.

                          ${shrugs}
                          • 10. Re: Handlers in JUnit tests?
                            807589
                            OP not interested in re-inventing the wheel, hence this question on the Forum. OP will explore runners and generated metrics. Ta. OP perceives <junitreport> as something a little too complex for now, but would appreciate links to good tutorials and examples for newbies. Ta.

                            J.
                            • 11. Re: Handlers in JUnit tests?
                              807589
                              Jrm wrote:
                              OP not interested in re-inventing the wheel
                              But several wheels already exist, and have been pointed to, to little avail!
                              hence this question on the Forum.
                              Common mistake: Asking a question on your idea for a solution to a problem, rather than on the problem itself. Your problem is that you need stats on what tests have passed and failed, but you asked how to add handlers to JUnit, believing that you need to do that to get the stats. There's almost certainly no need for you to do so.
                              OP will explore runners and generated metrics. Ta. OP perceives <junitreport> as something a little too complex for now, but would appreciate links to good tutorials and examples for newbies. Ta.
                              It might be a bit complex, but still simpler than writing handlers yourself. In any case, even the most basic text-based JUnit runner will already give you the stats you're after with no effort. Simply running the tests through the JUnit text runner, for examle, posts the results and stats back to the console. Other runners do similar things in their own UI arena
                              • 12. Re: Handlers in JUnit tests?
                                807589
                                George, you need to work on your communication skills and to learn not to draw conclusions when lacking information !!!

                                My 5 cents...

                                J.
                                • 13. Re: Handlers in JUnit tests?
                                  807589
                                  Jrm wrote:
                                  George, you need to work on your communication skills and to learn not to draw conclusions when lacking information !!!
                                  Perhaps you should offer some information when asking for help, then, and maybe not go around chastising people for trying to help you

                                  I'm out. Another snotty ingrate who's more concerned with manners-teaching than his problem. What a waste of time this place is turning into
                                  • 14. Re: Handlers in JUnit tests?
                                    807589
                                    Jrm wrote:
                                    My 5 cents...
                                    Poorly spent, in my opinion. George offered good advice, and you responded with personal attacks. That's not a good way to go about getting help at this forum. For your sake, I hope you adjust your behavior around here.

                                    Good luck.

                                    ~
                                    1 2 Previous Next