Create multiple parameter sets in one parameterized class (junit)

后端 未结 8 1030
无人共我
无人共我 2021-01-30 13:05

Currently I have to create a parameterized test class for every method that I want to test with several different inputs. Is there a way to add this together in one file?

<
8条回答
  •  渐次进展
    2021-01-30 13:10

    Another pure JUnit but yet elegant solution in my view is to encapsulate each parameterized test(s) in their own inner static class and use the Enclosed test runner on the top level test class. This allows you not only to use different parameter values for each test independently of each other but also to test methods with completely different parameters.

    This is how it would look like:

    @RunWith(Enclosed.class)
    public class CalculatorTest {
    
      @RunWith(Parameterized.class)
      public static class AddTest {
    
        @Parameters
        public static Collection data() {
          return Arrays.asList(new Object[][] {
              { 23.0, 5.0, 28.0 }
          });
        }
    
        private Double a, b, expected;
    
        public AddTest(Double a, Double b, Double expected) {
          this.a = a;
          this.b = b;
          this.expected = expected;
        }
    
        @Test
        public void testAdd() {
          assertEquals(expected, Calculator.add(a, b));
        }
      }
    
      @RunWith(Parameterized.class)
      public static class SubstractTest {
    
        @Parameters
        public static Collection data() {
          return Arrays.asList(new Object[][] {
              { 3.0, 2.0, 1.0 }
          });
        }
    
        @Parameter(0)
        private Double a;
        @Parameter(1)
        private Double b;
        @Parameter(2)
        private Double expected;
    
        @Test
        public void testSubstract() {
          assertEquals(expected, Calculator.substract(a, b));
        }
      }
    
      @RunWith(Parameterized.class)
      public static class MethodWithOtherParametersTest {
    
        @Parameters
        public static Collection data() {
          return Arrays.asList(new Object[][] {
              { 3.0, 2.0, "OTHER", 1.0 }
          });
        }
    
        private Double a;
        private BigDecimal b;
        private String other;
        private Double expected;
    
        public MethodWithOtherParametersTest(Double a, BigDecimal b, String other, Double expected) {
          this.a = a;
          this.b = b;
          this.other = other;
          this.expected = expected;
        }
    
        @Test
        public void testMethodWithOtherParametersTest() {
          assertEquals(expected, Calculator.methodWithOtherParametersTest(a, b, other));
        }
      }
    
      public static class OtherNonParameterizedTests {
    
        // here you can add any other test which is not parameterized
    
        @Test
        public void otherTest() {
          // test something else
        }
      }
    }
    

    Note the usage of the @Parameter annotation in the SubstractTest, which I consider more readable. But this is more a matter of taste.

提交回复
热议问题