Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit multiple setup and single test

Tags:

java

junit

I want to write a test that perform Setup in several ways but expect them to produce the same output. Basically like

@Before
public void setUp1(){
    obj.addDataThisWay(data);
}

@Before
public void setUp2(){
    obj.addDataThatWay(data);
}

@Test
public void testResult(){
    assertEquals(obj.getResult(),1);
}

I want it the test to run twice, one for setUp1()->testResult(), the other one for setUp2()->testResult() Is that possible?

like image 721
Wei Shi Avatar asked Jun 22 '11 15:06

Wei Shi


2 Answers

Not to my knowledge. You must either turn this into two separate tests (and extract the assertions to a common, private, non-@Test method if you want to), or you can use parameterized tests.

like image 122
Aasmund Eldhuset Avatar answered Sep 22 '22 05:09

Aasmund Eldhuset


public void testWithSetup1() {
     callSetup1Here();
     .....
}

public void testWithSetup2() {
     callSetup2Here();
     .....
}

I don't think there is any other way to do what you are asking.

like image 5
Rocky Pulley Avatar answered Sep 21 '22 05:09

Rocky Pulley