我正在尝试为这个包含两列的 CSV 文件编写规范器。BIN(第一行中的数字)和 BBL(第二行中的字符串)。然后我写了一个 JUnit 测试(在这个规范器下看到)。当我运行 Junit 测试时,我得到一个方法“初始化错误”未找到。打开测试班。有人可以帮帮我吗。
CSV 如下所示:
BIN boro
10001 Finnacial
...
归一化器
package bah.placebook.data.normalization.impl;
import java.util.HashMap;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import bah.placebook.data.models.Building;
import bah.placebook.data.normalization.util.LatLongUtil;
@Component
public class Neighborhood_Normalizer_CSV extends BuildingDataCSVNormalizer {
private static final Logger log = Logger.getLogger(Neighborhood_Normalizer_CSV.class);
@Override
public String getDatasetId(){
return"Neighborhood_data";
}
@Override
protected Building outputNormalizedRow(String[] csvRow){
Building building = new Building();
HashMap<String, Integer> columns = getColumnNames();
try {
building.setBin(Integer.parseInt(csvRow[columns.get("bin")]));
}
catch (NumberFormatException e) {
log.error("Unable to extract BIN number",e);
return null;
}
try {
String bbl = csvRow[columns.get("boro")] + csvRow[columns.get("block")] +
csvRow[columns.get("lot")];
building.setBblNumber(Integer.parseInt(bbl.trim()));
}
catch (NumberFormatException f) {
log.error("Unable to extract BBL number",f);
}
return building;
}
}
JUnit 测试
package bah.placebook.data.normalization.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.ResourceUtils;
import bah.placebook.data.normalization.NormalizationManager;
import bah.placebook.data.normalization.exceptions.UnknownDataSetException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/spring/application-context.xml")
public class NeighborhoodNormalizerTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Autowired
private NormalizationManager nManager;
@Test
public void normalize() throws UnknownDataSetException, IOException {
File testData = ResourceUtils.getFile("classpath:data/nieghborhood.csv");
File normalizedData = folder.newFile();
try(InputStream is = new FileInputStream(testData);
OutputStream os = new FileOutputStream(normalizedData)) {
nManager.normalizeDataset("nieghborhood",is , os);
}
try(BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(normalizedData)))) {
String line = reader.readLine();
while(line != null) {
System.out.println(line);
line = reader.readLine();
}
}
}
}