Java jUnit: Test suite code to run before any test classes
Asked Answered
S

5

21

I have a test suite class:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    GameMasterTest.class,
    PlayerTest.class,
})
public class BananaTestSuite { 

What annotation do I need to use to make a function in this class run before any of the classes containing actual tests? Right now, I'm doing this, and it works, but it's not as readable as it could be:

static {
    try {
        submitPeelAction = new Player(new GameMaster(1)).getClass().getDeclaredMethod("submitPeelAction");
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    submitPeelAction.setAccessible(true);
}

I tried @BeforeClass but it didn't work.

Subteen answered 27/12, 2009 at 22:56 Comment(3)
I think this works - throw it inside the static constructor perhaps ... Do not forget to document!Dotation
I think that none of your would-be answerers understand that you are trying to get some sort of suite-level Before.Sportswoman
An interesting answer have been provided here: https://mcmap.net/q/659699/-how-to-run-a-set-of-code-before-and-after-any-tests-in-any-classObligatory
S
24

Make a super test class containing the method annotated with @BeforeClass. Extend all the test classes from this class, eg,

@Ignore
public class BaseTest {
    @BeforeClass
    public static void setUpBaseClass() {
        //Do the necessary setup here
    }
}

This method will run before any @BeforeClass annotated method in the subclasses: Source. Ensure that this method name is not used in any subclass to prevent shadowing.

If you need to run this just once (eg, if you are creating/initializing a large resource for all tests to use), set a flag in the super class to check whether the method ran or not.

This design will also ensure that if you change the test runner, you need not change anything else.

Synchrocyclotron answered 30/6, 2014 at 10:13 Comment(0)
M
10

Use @Before for setUp() and @After for tearDown methods.

EDIT: after some tests here I found that @Before and @After does not work for Test Suite. In your case you should use @BeforeClass and a static method to encapsulate your initialization code.

@RunWith(Suite.class)
@SuiteClasses( { ContactTest.class })
public class AllTests {

    @BeforeClass
    public static void init() {
        System.out.println("Before all");
    }

}
Mamba answered 27/12, 2009 at 23:10 Comment(0)
W
3

First of all: if you are up to the latest JUnit, ie 4.7 or higher, you might also get this done with Rules. For a starting point see http://www.infoq.com/news/2009/07/junit-4.7-rules.

To my understanding, a static block in the suite class is a nice solution.

The downside is that it will only be called when your run the entire suit and not separate tests or test classes. So an alternative would be to call the same static method in the suite class from an @BeforeClass methods in all you classes.

Waits answered 28/12, 2009 at 0:18 Comment(0)
C
2

Annotate a method with @BeforeClass to make it run before all tests run in that class. Annotate a method with @Before to make it run before each test in that class.

re. your comments, @BeforeClass works fine. Are you throwing an exception within that method ?

Cowardly answered 27/12, 2009 at 23:25 Comment(2)
He doesn't want either of those, he wants something attached to the entire suite that runs before any of the BeforeClass methods.Sportswoman
Mmm. Perhaps you're right. I confess it doesn't seem that clear, though.Cowardly
E
1

try using @before and creating a method which instances all objects you need if you need to clear something after ending just use @After

something like this:

@Before
public void instanceMeasureList() {
    /* all what you need to execute before starting your tests
    */
}

@Test
public void testRequest() {
    fail("Not yet implemented");
}

@Test
public void testEpochDate() {
    fail("Not yet implemented");
}
@After 
public void deleteOutputFile() {
      fail("Not yet implemented");
}
Epicenter answered 26/10, 2015 at 16:4 Comment(1)
if you need more help go to: linkEpicenter

© 2022 - 2024 — McMap. All rights reserved.