private Reader createProperReader() throws IOException { String filepath = fileParameters.value(); String encoding = fileParameters.encoding(); if (filepath.indexOf(':') < 0) { return new InputStreamReader(new FileInputStream(filepath), encoding); } String protocol = filepath.substring(0, filepath.indexOf(':')); String filename = filepath.substring(filepath.indexOf(':') + 1); if ("classpath".equals(protocol)) { return new InputStreamReader(getClass().getClassLoader().getResourceAsStream(filename), encoding); } else if ("file".equals(protocol)) { return new InputStreamReader(new FileInputStream(filename), encoding); } throw new IllegalArgumentException("Unknown file access protocol. Only 'file' and 'classpath' are supported!"); }
private Object[] paramsFromFile() { try { Reader reader = createProperReader(); DataMapper mapper = fileParameters.mapper().newInstance(); try { return mapper.map(reader); } finally { reader.close(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( "Could not successfully read parameters from file: " + fileParameters.value(), e); } }
@Test //@FileParameters(value = "/home/tomek/book/github/put_2/src/test/resources/com/practicalunittesting/chp06/excel/financial_junitparams.csv", mapper= CsvWithHeaderMapper.class) @FileParameters(value = "classpath:com/practicalunittesting/chp06/excel/financial_junitparams.csv", mapper= CsvWithHeaderMapper.class) public void shouldCalculateDiscount(String value, String discount) { assertEquals(Double.parseDouble(discount), DiscountCalculator.calculateDiscount(Double.parseDouble(value)), 0.0001); }
@Test //@FileParameters(value = "/home/tomek/book/github/put_2/src/test/resources/com/practicalunittesting/chp06/excel/financial_junitparams.csv", mapper= CsvWithHeaderMapper.class) @FileParameters(value = "classpath:com/practicalunittesting/chp06/excel/financial_junitparams.csv", mapper= CsvWithHeaderMapper.class) public void shouldCalculateDiscountDoubles(double value, double discount) { assertEquals(discount, DiscountCalculator.calculateDiscount(value), 0.0001); } }