Is it possible to parameterize a testing method, rather than an entire class?

As I understand it, with JUnit 4.x and its annotation, org.junit.runners.ParameterizedI can make my unit test “parameterized”, which means that for each set of parameters, if all unit test will be executed again, from scratch.

This approach limits me because I cannot create a “parameterized method”, for example:

..
@Test 
public void testValid(Integer salary) {
  Employee e = new Employee();      
  e.setSalary(salary);
  assertEqual(salary, e.getSalary());
}
@Test(expected=EmployeeInvalidSalaryException.class) 
public void testInvalid(Integer salary) {
  Employee e = new Employee();      
  e.setSalary(salary);
}
..

As you can see from the example, I need two collections of parameters in one unit test. Can this be done in JUnit 4.x? This is possible in PHPUnit, for example.

ps. Maybe it is possible to implement such a mechanism in some other unit testing system, and not in JUnit?

+3
3

(, , ) , .

, . , ( ).

private Integer salary;
private boolean valid;

@Test 
public void testValid() {
  if (valid) {
    Employee e = new Employee();      
    e.setSalary(salary);
    assertEqual(salary, e.getSalary());
  }
}

@Test(expected=EmployeeInvalidSalaryException.class) 
public void testInvalid() {
  if (!valid) {
    Employee e = new Employee();      
    e.setSalary(salary);
  }else {
    throw new EmployeeInvalidSalaryException();
  }
}

JUnit , , .

+1

, . zohhak. :

@TestWith({
   "25 USD, 7",
   "38 GBP, 2",
   "null,   0"
})
public void testMethod(Money money, int anotherParameter) {
   ...
}
+1

Source: https://habr.com/ru/post/1766000/


All Articles