Automatically populate JUnit settings and debugging

For the good part of the tests that I run for my application, I have a common set of settings and tearDown.

When I create a new JUnit Test Case class, I have the opportunity for eclipse to automatically create the setUp () and tearDown () methods.

Is there a way to change the default setUp and TearDown methods for some custom lines of code?

So, when I create a new test class with the setUp method checked, I get:

@Before
public void setUp() throws Exception
{
    UnitTestSetup.standardSetup();
}

Importing should not be a problem, as it automatically imports the required package upon saving.

+4
source share
4 answers

JUnit Test Case Eclipse, . .

        buffer.append("void "); //$NON-NLS-1$
        buffer.append(methodName);
        buffer.append("() throws "); //$NON-NLS-1$
        buffer.append(imports.addImport("java.lang.Exception")); //$NON-NLS-1$
        buffer.append(" {}"); //$NON-NLS-1$
        buffer.append(delimiter);

JUnit . @Before @After - :

public class Common {
     @Before
     public void setUp() {
         ...
     }
     @After
     public void tearDown() {
         ...
     }
}

public class MyTest extends Common {
    @Test
    public void test() {
        // Common.setUp() will have been run before this test
        // Common.tearDown() will run after the test
    }
}
+2

, . , @Test, , , @VerySpecialTestWithGeneratedSetupAndTeardown /, .

, , , : http://hannesdorfmann.com/annotation-processing/annotationprocessing101/

+1

, , , . , - - .

- :

-:

package com.foo.bar.businesslogic.woozinator;

public class Woozinator {

    public void doSomething() {
        System.out.println("Doing something.");
        System.out.println("Done.");
    }

}

:

package com.foo.bar.businesslogic.woozinator;

import org.junit.Test;

import com.foo.bar.businesslogic.testutil.WidgitUserTestSetupUtil;
import com.foo.bar.businesslogic.testutil.WidgitUserTestTearDownUtil;

public class WoozinatorTest {

    @Test
    public void shouldPassTest() {
        WidgitUserTestSetupUtil.setUp();
        // do test stuff here
        WidgitUserTestTearDownUtil.tearDown();
    }

}

:

package com.foo.bar.businesslogic.testutil;

public class WidgitUserTestSetupUtil {

    public static void setUp() {
        System.out.println("Setting up widget environment for widget user test");
        // do stuff here
        System.out.println("Done setting up environment");
    }

}

:

package com.foo.bar.businesslogic.testutil;

public class WidgitUserTestTearDownUtil {

    public static void tearDown() {
        System.out.println("Doing woozinator tear down.");
        // undo stuff here
        System.out.println("Done with woozinator tear down.");
    }

}
0

JUnit, TestWatcher starting finished.

public class YourRule extends TestWatcher {
  @Override
  protected void starting(Description description) {
    UnitTestSetup.standardSetup();
    //more of your code
  }

  @Override
  protected void finished(Description description) {
    //your code
  }
}

:

public class YourTest {
  @Rule
  public final YourRule yourRule = new YourRule();

  ...
}
0

All Articles