سؤال

I am interested in getting ideas on how can I design my junit test cases so that they are more readable and maintainable. Here is the problem - In all my junit test cases I have some predefined steps to be executed like below

public class MyTestClass {

private Service myService;

@Test
public void testCase1() {
    mockObjectX("A");
    mockObjectY(1);
    mockObjectZ("M", 1);

    myService.validate("value1", "value2");

}

@Test
public void testCase2() {
    mockObjectX("Q");
    mockObjectY(5);
    mockObjectZ("Z", 10);

    myService.validate("value3", "value4");

}


private void mockObjectZ(String value1, int value2) {
    //mock object Z
}

private void mockObjectY(Integer value) {
    //mock object Y
}

private void mockObjectX(String value) {
    //mock object X
}
}

The issue is if I were to add a new test case I need make sure that the new test case calls mockObjectX(), mockObjectY(), mockObjectZ(). Is there a better way (a more maintainable way) of designing my test cases such that adding new testcases becomes simpler?

I think that @Before cannot be used since the mock* methods accept parameters.

One way I could think of is having a class containing methods for each parameter value required to mock object X, Y and Z

public class MyTestClass {

private Service myService;

@Test
public void testCase1() {
    mockObjects(new TestCase1());
    myService.validate("value1", "value2");

}

@Test
public void testCase2() {
    mockObjects(new TestCase2());
    myService.validate("value3", "value4");

}

private void mockObjects(ServiceObjectMocker serviceObjectMocker) {
    //mock object x, y and z by calling respective methods from serviceObjectMocker   
}  

private class ServiceObjectMocker {

    protected Object[] getValuesForObjectZ() {
        //pass default values
        return new Object[] {};     
    }

    protected Integer getValuesForObjectY() {
        //pass default values
        return 1;
    }

    protected String getValuesForObjectX() {
        //pass default values
        return "A";
    }

}

private class TestCase1 extends ServiceObjectMocker {
    @Override
    protected String getValuesForObjectX() {
        return "B";    
    }  
}

private class TestCase2 extends ServiceObjectMocker {
    @Override
    protected String getValuesForObjectX() {
        //pass different values
        return "Q";
    }

    @Override
    protected Integer getValuesForObjectY() {
        //pass default values
        return 10;
    }

}
}

This reduces the no. of method calls each test case needs to make. Any better ideas?

هل كانت مفيدة؟

المحلول

If you really area doing the same thing each time, it's just the data that varies, then look at @Parameterized.

You could then have something like (untested and uncompiled):

@RunWith(Parameterized.class)
public class MyTestClass {
  @Parameters
  public static List<Object[]> data() {
      return Arrays.asList(new Object[][] {
          { "A", 1, "M", 1, "value1", "value2" },
          { "Q", 5, "Z", 10, "value3", "value4" },
      });
  }

  private String xValue;
  private int yValue;
  private String zValueString;
  private int zValueInt;
  private String expected1;
  private String expected2;

  public MyTestClass(String xValue, int yValue, String zValueString, int zValueInt, String expected1, String expected2) {
    this.xValue = xValue;
    this.yValue = yValue;
    this.zValueString = zValueString;
    this.zValueInt = zValueInt;
    this.expected1 = expected1;
    this.expected2 = expected2;
  }

  @Test
  public void test() {
    mockObjectX(xValue);
    mockObjectY(yValue);
    mockObjectZ(zValueString, zValueInt);

    myService.validate(expected1, expected2);
  }
}

You are then only defining your test once.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top