单元测试构造函数方法

时间:2020-02-13 12:11:50

标签: java junit4

我难以理解如何对构造方法进行单元测试。

我需要检查是否抛出了错误。构造函数是:

@Autowired
public BankDetailsValidator() {
  try {
    logDebugMessage("BankDetailsValidator() constructor");
    loadModulusWeightTable();
    loadSortCodeSubstitutionTable();
  } catch (final IOException e) {
    throw new BankDetailsValidationRuntimeException("An error occured loading the modulus weight table or sort code substitution table", e);
  }
}

要对此进行测试,我需要掷loadModulusWeightTableloadSortCodeSubstitutionTable并掷IOException

private void loadModulusWeightTable() throws IOException {
  modulusWeightTable.clear();
  logDebugMessage("Attempting to load modulus weight table " + MODULUS_WEIGHT_TABLE);

  final InputStream in = new FileInputStream(MODULUS_WEIGHT_TABLE);
  br = new BufferedReader(new InputStreamReader(in));
  try {
    String line;
    while ((line = br.readLine()) != null) {
      final String[] fields = line.split("\\s+");
      modulusWeightTable.add(new ModulusWeightTableEntry(fields));
    }
    logDebugMessage("Modulus weight table loaded");
  }
  finally {
    br.close();
  }
}

我试图使用Spy来使缓冲文件阅读器返回一个IOException,但是由于它在构造函数中而无法使它工作。

public class BankDetailsValidatorTest {

  @Spy
  private BufferedReader mockBufferReader;

  @InjectMocks
  private CDLBankDetailsValidator testSubject;

  @Test(expected = IOException.class)
  public void testIOErrorLogging() throws Exception{

    when(mockBufferReader.readLine()).thenThrow(new IOException());
    testSubject = new CDLBankDetailsValidator();
  }
}

2 个答案:

答案 0 :(得分:0)

我认为应该重构BankDetailsValidator类。在这种情况下,您应该提取负责读取数据的代码以分离类,并将其作为构造函数参数注入到BankDetailsValidator中。之后,您可以分别测试该读取器,当然也可以使用模拟的读取器测试BankDetailsValidator。

答案 1 :(得分:0)

这是重构代码的方法:

@Autowired
public BankDetailsValidator(Collection<ModulusWeightTableEntry> table) {
  try {
    logDebugMessage("BankDetailsValidator() constructor");
    this.modulusWeightTable.addAll(table);
    loadModulusWeightTable();
    loadSortCodeSubstitutionTable();
  } catch (final IOException e) {
    throw new BankDetailsValidationRuntimeException("An error occured loading the modulus weight table or sort code substitution table", e);
  }
}

这可以将文件部分的负载删除到@Configuration上。

@Configuration
class WeightTableConfiguration {
    @Bean
    ModulusWeightTable loadFromFile(@Value("${mwt.filename}") String filename) {
      Collection<ModulusWeightTableEntry> table = new ArrayList<>();
      logDebugMessage("Attempting to load modulus weight table " + filename);

      try (InputStream in = new FileInputStream(filename);
           br = new BufferedReader(new InputStreamReader(in))) {
        String line;
        while ((line = br.readLine()) != null) {
          final String[] fields = line.split("\\s+");
          table.add(new ModulusWeightTableEntry(fields));
        }
        logDebugMessage("Modulus weight table loaded");
      }
      return table;
    }
}

现在,您可以与加载代码分开测试验证器,并创建一个单独的测试配置,以提供手动创建的重量表。

或者,您可以使用Supplier<WeightTable>参数化构造函数,然后将加载代码放入实现get()类的@Component方法中。