1

This is the sample JUnit test code included in JUNIT4. It shows two cases: the type safe way and the dynamic way. I tried to use the type safe way.

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Some simple tests.
 */
public class SimpleTest extends TestCase {
    protected int fValue1;
    protected int fValue2;

    SimpleTest(String string)
    {
        super(string);
    }

    @Override
    protected void setUp() {
        fValue1 = 2;
        fValue2 = 3;
    }

    public static Test suite() {

          /*
           * the type safe way
           */
          TestSuite suite= new TestSuite();
          suite.addTest(
              new SimpleTest("add") {
                   protected void runTest() { testAdd(); }
              }
          );

          suite.addTest(
              new SimpleTest("testDivideByZero") {
                   protected void runTest() { testDivideByZero(); }
              }
          );
          return suite;
    }

    ...

    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}

I'm not sure about this code snippet.

suite.addTest(
    new SimpleTest("add") {
        protected void runTest() { testAdd(); }
        }
    );
  • How does new Simple("add") {...} works? I mean, how code block can follow the new operator?
  • Why {...} block is needed? I tried without it, and I got no compilation/runtime errors.
prosseek
  • 182,215
  • 215
  • 566
  • 871

2 Answers2

2

In this fragment:

suite.addTest(
    new SimpleTest("add") {
        protected void runTest() { testAdd(); }
    }
);

you create an anonymous inner class and pass it as parameter to the addTest method. You called the SimpleTest constructor that takes String as a parameter so you need to add another constructor.As for Q3 question, TestCase has also no-arg constructor so this code would be correct without adding additional contructor:

suite.addTest(
    new SimpleTest() {
        protected void runTest() { testAdd(); }
    }
);
Community
  • 1
  • 1
kaos
  • 1,598
  • 11
  • 15
0

I have a simple example of the creation and usage Anonymous Inner Class. To me Inner is a little bit misleading, as it doesn't seem to use inner class, but just changing some of the method actions.

class Ferrari {
    public void drive() {
        System.out.println("Ferrari");
    }
}

// Example 1: You can modify the methods of an object.
class Car {
    Ferrari p = new Ferrari() {
        public void drive() {
            System.out.println("anonymous Ferrari");
        }
    };
}

class AnonymousAgain {
    public void hello(Ferrari car) {
        car.drive();
    }

    public static void main(String[] args) {
        AnonymousAgain a = new AnonymousAgain();
        // Example 2
        a.hello(new Ferrari() {
            public void drive() {
                System.out.println("The power of inner");
            }
        });
    }
}
prosseek
  • 182,215
  • 215
  • 566
  • 871