Junit set up once for whole integration tests(all test classes) - java

I have some own components, which I start before my Java application. It takes about 30 seconds to start this. In my integration tests, I start my components before the class and all test cases run. My question is, is it possible to run my components not before the test class, but rather before the whole test?
kind regards,
bilal

If you use JUnit suites you can use a #BeforeClass to execute a method before the entire suite runs and an #AfterClass after the entire suite runs:
#RunWith(Suite.class)
#SuiteClasses(
{
//list your test classes here
}
)
public class IntegrationSuite{
#BeforeClass
public static void setupSuite(){
//do your initialization here
}
#AfterClass
public static void tearDownSuite(){
//...if needed
}
}

Use the #BeforeClass annotation.
Please note that the annotated method has to be static.
#BeforeClass
public static void oneTimeInit() {
System.out.println("It runs only once for all tests in this class.");
}

If you are using maven you could use the maven-failsafe-plugin. It has a pre-integration-test goal intended for test setup. For an example take a look at Maven Failsafe Plugin: how to use the pre- and post-integration-test phases

Related

Junit5 never hitting #BeforeAll inside #Suite class

Currently going through the process of upgrading from Junit4 to Junit5 and running into a bit of a hurdle with something that used to work in Junit4.
Wondering if there is a way to access the #BeforeAll/#AfterAll (formerly #BeforeClass/#AfterClass) within a class which is also a #Suite
Example:
#Suite
#SelectClasses({
SquareService.class,
TriangleService.class
})
public class ShapeTestSuite {
#BeforeAll
public void beforeAll() {
Log.info("Shape Test Suite Start!");
}
#AfterAll
public void afterAll() {
Log.info("Shape Test Suite End!");
}
}
I've tried adding an extension to this class/suite, but that also does not seem to work. What am I missing? Any help would be greatly appreciated.
Thanks
#BeforeAll and #AfterAll runs before any execution of #Test, #TestFactory, etc.
So if you create one them you will see that your code will be run correctly.
#BeforeAll and #AfterAll are annotations from Jupiter, which is one (of many) JUnit 5 engines. #Suite, however, is independent and can run tests and test suites combined from any JUnit 5 engine. That’s why you cannot use a before-all method in a suite class.

Allure java Run specific tests depending on story or feature

I have two methods in my test class:
#Test
#Stories( "story1")
public void test01(){
}
#Test
#Stories( "story2")
public void test02(){
}
#Test
#Stories( "story1")
public void test03(){
}
To run tests Im using:
mvn clean test site
It will execute all test. But my question is, how to execute tests when I want to execute only tests with specific user story (ie. story1)
I know in python it can be done by
py.test my_tests/ --allure_stories=story1
But I don't know how to do it in java using maven
In Java there is no need for Allure to do such sort of things, because you can do it using your test runner, e.g. TestNG.
Just create Listener or BeforSuite which will check your environment variable e.g. -DallureStories and match it with ITestContext to disable tests not in your stories list.

Share object reference between different JUnit tests

I have a couple of JUnit tests which need a reference for a expensive resource (a WALA class hierachie), which needs about 30s to be created. I would like to share this reference in my whole test suite.
I thought about a static member in a base class which is laziely initiated with a #BeforeClass method. After test is run the JVM should be determined anyway.
Is there any other way to accomplish this? Or any other best practice?
Create an explicit test suite (cf. this answer) to run these tests, and use #BeforeClass and #AfterClass on the suite itself (cf. this answer):
#RunWith(Suite.class)
#Suite.SuiteClasses({Test1.class, Test2.class})
public class MySuite {
#BeforeClass
public static void initResource() {
MyExpensiveResource.init();
}
#AfterClass
public static void disposeResource() {
MyExpensiveResource.dispose();
}
}

JUnit: Is there any difference between running a Suite, and running tests individually?

I am having a problem, one test class seems to be interfering with the other in my test suite.
I have a suite, which executes two classes, one is called MergeTestSuite.java (which is another Suite), and the other is called RecordTest.java.
RecordTest extends one class already tested by MergeTestSuite.java
I created another suite as follows:
#RunWith(Suite.class)
#Suite.SuiteClasses( {
MergeTestSuite.class,
RecordTest.class
})
public class CoreTestSuite {
#BeforeClass
public static void install() throws Throwable {
RegistryUtils.cleanupResources();
}
}
Both MergeTestSuite.class and RecordTest.class run fine individually. If I run CoreTestSuite, the second test will fail, unless I remove MergeTestSuite.class from the list.
A Junit TestSuite provides extra features for multiple tests. For instance, you can control the order in which the tests run and you can also combine multiple test suites into another suite. I think this older doc from Junit 3.1.8 describes it best.

How do I make a junit white list of tests to run

I'm working on writing unit tests for a class that I'm developing. Another developer is developing other tests for the same class for methods that he's developing. So our tests find themselves in the same JUnit test class.
So what I wanted to do was to set up a test suite to run just my tests while I'm developing as a temporary measure. I created a Category for my tests and have marked them as such. I then created a class to be my test suite. I told it to include tests that belong to this category. When I run it, it still runs everything. There are a lot of tests, so it would be tedious to mark all the tests I don't want ran with #Ignore. Is there a way to say, run only the tests in a category but none else?
You can write a wrapper test class which method calls the main test class (only your method), then run Junit tests on the wrapper class.
public class MainTestClass {
#Test
public void yourFirstTest() {
...
}
#Test
public void yourSecondTest() {
...
}
#Test
public void otherFirstTest() {
...
}
}
public class WrapperTestClass {
#Test
public void yourFirstTest() {
new MainTestClass().yourFirstTest();
}
#Test
public void yourSecondTest() {
new MainTestClass().yourSecondTest();
}
}
I think you can implement your own 'org.junit.runner.RunWith' and then annotate your test class to use it as necessary.
#RunWith(MyRunnerClass.class)
Note: The correct solution here is in the above comments regarding code branches etc.

Categories

Resources