java—对构造函数方法进行单元测试

yizd12fk  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(520)

我很难理解如何对构造函数方法进行单元测试。
我需要检查是否抛出了错误。建造师是:

@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);
  }
}

为了测试这个,我需要 loadModulusWeightTable 或者 loadSortCodeSubstitutionTable 投掷 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();
  }
}
zxlwwiss

zxlwwiss1#

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

yrdbyhpb

yrdbyhpb2#

下面是如何重构代码:

@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 班级。

相关问题