简体   繁体   中英

Repeat JUnit tests run with different setup

For example I have some tests based on Set<Integer> . I want to run them with TreeSet and then with HashSet . Can I do it without manual initialization inside test method body?

Something like this:

public class SomeTest {
    Set<Integer> set;

    @Before 
    public void init() {
        set = new HashSet<>();
    }

//    @Before
//    public void init2() {
//        set = new TreeSet<>();
//    }

    //test...
}

I want to run all tests with init() first and then with init2() . How can I do it?

A cleaner approach would be:

public abstract class SomeTestsForSets {
  Set<Integer> set;

  @Before 
  public abstract void init();

  //test cases...
}

public class HashSetTests extends SomeTestsForSets {

  @Override
  public void init() {
    this.set = new HashSet<>();
  }
}

public class TreeSetTests extends SomeTestsForSets {

  @Override
  public void init() {
    this.set = new TreeSet<>();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM