Junit before class (not static)

Are there any recommendations for Junit to execute the function once in the test file, and it should not be static either.

like @BeforeClass for a non-static function?

Here is an ugly solution:

 @Before void init(){ if (init.get() == false){ init.set(true); // do once block } } 

Well, this is what I don't want to do, and I'm looking for an integrated junit solution.

+72
java junit
May 13 '10 at 9:22 am
source share
8 answers

If you do not want to install static initializers for a one-time initialization and are not particularly good at using JUnit, see TestNG. TestNG supports non-stationary one-time initialization with various configuration parameters using annotations.

In TestNG, this would be equivalent to:

 @org.testng.annotations.BeforeClass public void setUpOnce() { // One time initialization. } 

For separation

 @org.testng.annotations.AfterClass public void tearDownOnce() { // One time tear down. } 

For the TestNG equivalent for JUnit 4 @Before and @After you can use @BeforeMethod and @AfterMethod respectively.

+18
May 13, '10 at 17:35
source share

Using an empty constructor is the easiest solution. You can still override the constructor in the extended class.

But this is not optimal with all inheritance. This is why JUnit 4 uses annotations instead.

Another option is to create a helper method in the factory / util class and let this method do its job.

If you use Spring, you should consider using the @TestExecutionListeners annotation. Something like this test:

 @RunWith(SpringJUnit4ClassRunner.class) @TestExecutionListeners({CustomTestExecutionListener.class, DependencyInjectionTestExecutionListener.class}) @ContextConfiguration("test-config.xml") public class DemoTest { 

Spring AbstractTestExecutionListener contains, for example, this empty method, which you can override:

 public void beforeTestClass(TestContext testContext) throws Exception { /* no-op */ } 

NOTE: DO NOT skip or skip DependencyInjectionTestExecutionListener when adding custom TestExecutionListeners . If you do this, all autowires will be null .

+34
May 13 '10 at 10:29
source share

A simple if statement also works very well:

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:test-context.xml"}) public class myTest { public static boolean dbInit = false; @Autowired DbUtils dbUtils; @Before public void setUp(){ if(!dbInit){ dbUtils.dropTables(); dbUtils.createTables(); dbInit = true; } } ... 
+34
May 17 '12 at 16:04
source share

Easily use the @BeforeAllMethods / @AfterAllMethods to run the method inside the instance context (non-static), where all entered values ​​will be available.

There is a special testing library for this:

https://mvnrepository.com/artifact/org.bitbucket.radistao.test/before-after-spring-test-runner/0.1.0

https://bitbucket.org/radistao/before-after-spring-test-runner/

The only limitation: it works only for spring testing.

(I am the developer of this testing library)

+7
Sep 18 '17 at 8:36 on
source share

I have never tried, but maybe you can create a constructor without arguments and call a function from there?

0
May 13 '10 at 9:30 a.m.
source share

UPDATE: Please see Cherry's comment on why the suggestion below is erroneous. (I am keeping the answer here, not deleting it, as a comment may provide useful information to others about why this does not work.)




Another option worth considering if you use dependency injection (e.g. Spring) is @PostConstruct . This will ensure that dependency injection is complete, which will not occur in the constructor:

 @PostConstruct public void init() { // One-time initialization... } 

0
Feb 27 '14 at 10:35
source share

The article discusses 2 very pleasant solutions to this problem:

  • "clean" junit with a custom Runner (using the interface, but you can extend it with a special annotation like @BeforeInstance).
  • Spring performers mentioned earlier in Espen.
0
Oct. 15 '14 at 20:28
source share

Just use @BeforeClass :

 @BeforeClass public static void init() { } 

It makes no sense for init be non-static, because each test runs in a separate instance. The instance that init is running will not match the instance of any test.

The only reason you want it to be non-static is to override it in subclasses, but you can do it with static methods too. Just use the same name and only the init subclass method will be called.

0
Jan 01 '15 at 12:23
source share



All Articles