ttt307307 / junitparams

Automatically exported from code.google.com/p/junitparams
0 stars 0 forks source link

Test classes instantiated twice for every test method #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
/* Often class member declarations and initializers are used in test classes as 
a de-facto @Before method, which is possible thanks to some JUnit 
implementation details. Preserving this behavior could save some users who 
didn't RTFM from being discouraged from using junitparams. */

//a motivational example:

package pl.gajowy;

import junitparams.JUnitParamsRunner;
import org.hamcrest.core.Is;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

@RunWith(JUnitParamsRunner.class)
public class SingleInstantiationMotivatingTest {

    WebDriver driver = new InstantiateMeOnceWebDriver();

    @Test
    public void classBodyAndInitializersShoulStayAsAnAlternativeForBeforeMethod() {
        final int numberOfTestsExecutedSoFar = 1;
        assertThat(InstantiateMeOnceWebDriver.instantiationCounter, is(numberOfTestsExecutedSoFar));
    }

    interface WebDriver {
    }

    private static class InstantiateMeOnceWebDriver implements WebDriver {
        private static int instantiationCounter = 0;

        private InstantiateMeOnceWebDriver() {
            instantiationCounter++;

            if (instantiationCounter > 1) {
                throw new IllegalStateException("Kaboom!");
            }
        }
    }
}

Original issue reported on code.google.com by arturgaj...@gmail.com on 13 Sep 2011 at 10:12

GoogleCodeExporter commented 8 years ago
fix available at 
http://code.google.com/r/arturgajowy-junitparams/source/detail?r=b9d6ae68ed5364e
5b7140554a13370be1d80ce3a

Original comment by arturgaj...@gmail.com on 13 Sep 2011 at 10:21

GoogleCodeExporter commented 8 years ago
Thanks a lot. Done.

Original comment by lipinski...@gmail.com on 17 Nov 2011 at 10:22