문제

I'd like a junit runner that executes all @Before methods, then all @Test methods, then all @After methods.

This is how my System-Tests work. The @Before methods are run, to setup the test data and scenarios. The application is started. Then the @Test methods are run with the application running in the background. Those @Test methods can change data or respond to the application. Then the framework waits for the application to finish up. Afterward, the @After methods are run to verify the test results.

I already use junit annotations, assertion methods, and other various bits. But I just can't figure out how to use junits runners to execute test methods in this way. I couldn't make heads nor tails of the "Computer" interface in junit 4.8, or figure out how to apply Rules to this.

도움이 되었습니까?

해결책

This isn't what JUnit does. JUnit has a design philosophy that emphasizes independent unit tests. As such, it isn't a natural framework for system tests. What you want to do fits nicely into TestNG (which as a design goal tries to straddle both unit and system tests).

In JUnit the @Before and @After are run before and after each test. You can shoehorn this kind of thing into JUnit using a Suite, which references all of your tests and is responsible for all setup and teardown, so the Suite's @BeforeClass and @AfterClass methods get run before and after the suite, which if you organize it correctly could be all of your system tests.

There are lot of organizational challenges in the code when it gets large with the JUnit approach, so I would suggest you consider and alternative framework if this is the bulk of what you want to do.

다른 팁

I think you can solve this by making only one actual test method, that just calls are your actual tests, which you do not declare as ssuch.

Kinda like:

@Before
public void beforeTest(){}

@After
public void afterTest(){}

@Test
public void test(){
    test1();
    test2();
    test3();
}

public void test1(){}

public void test2(){}

public void test3(){}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top