Loading ...
Sorry, an error occurred while loading the content.

running tests with anonymous inner classes

Expand Messages
  • craig@frontloader.net
    The JUnitTest Infected article alludes to using anonymous inner classes for running single tests. I can t get this to work. I don t understand where to put
    Message 1 of 4 , Aug 24, 2001
    View Source
    • 0 Attachment
      The JUnitTest Infected article alludes to using anonymous inner
      classes for running single tests. I can't get this to work. I don't
      understand where to put the anonymous inner class. In each testXXX
      method? The doc says, "override the runTest method... and call the
      desired test case. A convenient way to do this is with an anonymous
      inner class." OK, where does this class go? Then how do I run it?
      IE "java <what class?>".

      Another alternative given for a single test is to execute "TestResult
      result= (new MoneyTest("testMoneyMoneyBag")).run();" Again, how do I
      execute this? ie, where do I put this line of code? Whose main
      method do I call?

      I have no problem running tests by simply invoking the TestRunner
      tools or by using main methods in my TestCase classes, but I have no
      concept for how to do the "simpler" single test scenarios. Can
      someone please explain with a few lines of code?

      -Thanks Craig
    • Johannes Link
      ... new TestCase( mytest ) { public void runTest() { //here s your test code } }.run(); This code can be executed wherever you like. ... The authors have
      Message 2 of 4 , Aug 24, 2001
      View Source
      • 0 Attachment
        > The JUnitTest Infected article alludes to using anonymous inner
        > classes for running single tests. I can't get this to work. I don't
        > understand where to put the anonymous inner class. In each testXXX
        > method? The doc says, "override the runTest method... and call the
        > desired test case. A convenient way to do this is with an anonymous
        > inner class." OK, where does this class go? Then how do I run it?
        > IE "java <what class?>".

        new TestCase("mytest") {
        public void runTest() {
        //here's your test code
        }
        }.run();

        This code can be executed wherever you like.

        > Another alternative given for a single test is to execute "TestResult
        > result= (new MoneyTest("testMoneyMoneyBag")).run();" Again, how do I
        > execute this? ie, where do I put this line of code? Whose main
        > method do I call?

        The authors have probably some kind of workspace in mind where they can
        directly run any piece of Java code - as e.g. in VisualAge. Not having
        such a workspace you have to build your own main-method somewhere.

        Hope that helps,
        Johannes
      • craig@frontloader.net
        Then how do I run it? ... new TestCase( mytest ) { public void runTest() { //here s your test code } }.run(); This code can be executed wherever you like. The
        Message 3 of 4 , Aug 24, 2001
        View Source
        • 0 Attachment
          Then how do I run it?
          > IE "java <what class?>".

          new TestCase("mytest") {
          public void runTest() {
          //here's your test code
          }
          }.run();

          This code can be executed wherever you like.

          The authors have probably some kind of workspace in mind where they
          can directly run any piece of Java code - as e.g. in VisualAge. Not
          having such a workspace you have to build your own main-method
          somewhere.

          Hope that helps,
          Johannes

          Yes, perhaps thats my confusion. In response to your email I
          created a class that just creates TestCase instances and calls the run
          () or runTest() methods. While it compiles and runs them, it is
          pointless since there is no TestRunner to interpret and display the
          results of running these TestCase instances.

          What then is the best practice for running Tests? Is it using a main
          method in the TestCase class that contains:
          junit.awtui.TestRunner.run(<classname>.class);

          or is it better to keep a TestRunner up and running and load TestCase
          classes?

          Thanks,
          Craig
        • Johannes Link
          ... Having a test runner up saves you the time to start it up on every test run. Having it in a main method or elsewhere is sometimes needed for automating the
          Message 4 of 4 , Aug 24, 2001
          View Source
          • 0 Attachment
            > What then is the best practice for running Tests? Is it using a main
            > method in the TestCase class that contains:
            > junit.awtui.TestRunner.run(<classname>.class);
            >
            > or is it better to keep a TestRunner up and running and load TestCase
            > classes?

            Having a test runner up saves you the time to start it up on every test
            run.
            Having it in a main method or elsewhere is sometimes needed for
            automating the execution or to provide a standard entry point.

            BTW, the run method returns a TestResult object which can be queried
            about errors and failures.

            Johannes
          Your message has been successfully submitted and would be delivered to recipients shortly.