我正在用Mockito學習JUnit。我對如何開始以及如何爲下面的課程編寫單元測試有所瞭解。這將是很高興,如果有人能幫助我在寫他們:如何編寫低於類的junit測試用例
@Service
public class GetDataFromHistDataImpl implements GetDataFromHistData{
@Override
public File downloadData(String formUrl) {
String tk = null;
String date= null;
String datemonth= null;
String platform= null;
String timeframe= null;
String fxpair= null;
WebClient webClient=new WebClient();
try
{
HtmlPage page1=webClient.getPage(formUrl);
webClient.getOptions().setJavaScriptEnabled(false);
HtmlForm form=page1.getFormByName("file_down");
tk=form.getInputByName("tk").getAttribute("value");
date=form.getInputByName("date").getAttribute("value");
datemonth=form.getInputByName("datemonth").getAttribute("value");
platform=form.getInputByName("platform").getAttribute("value");
timeframe=form.getInputByName("timeframe").getAttribute("value");;
fxpair=form.getInputByName("fxpair").getAttribute("value");
System.out.println("fx"+fxpair);
webClient.close();
}
catch (IOException e1) {
e1.printStackTrace();
}
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target("http://www.histdata.com/get.php");
MultivaluedMap<String, String> map = new MultivaluedStringMap();
map.add("tk", tk);
map.add("date", date);
map.add("datemonth", datemonth);
map.add("platform", platform);
map.add("timeframe", timeframe);
map.add("fxpair", fxpair);
InputStream responseStream = target.request().header("Referer", "http://www.histdata.com/")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
.header("Accept-Encoding", "tar, deflate")
.post(Entity.form(map), InputStream.class);
File file = new File("data.tar");
OutputStream fos;
try
{
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = responseStream.read(buffer)) != -1)
{
fos.write(buffer, 0, len);
}
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return file;
}
@Override
public File unZipData(File zipFile) {
byte[] buffer = new byte[1024];
final String OUTPUT_FOLDER = "/Users/venkateswara/Downloads/Sindhu/histdata";
File returnFileName=null;
try{
File folder = new File(OUTPUT_FOLDER);
if(!folder.exists()){
folder.mkdir();
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
if(ze==null)
{
System.out.println("ZipFile is empty or corrupted");
System.exit(0);
}
while(ze!=null){
String fileName = ze.getName();
File newFile = new File(OUTPUT_FOLDER + File.separator + fileName);
if(FilenameUtils.getExtension(newFile.getName()).contains("csv"))
{
returnFileName=newFile.getAbsoluteFile();
}
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
catch(IOException ex){
ex.printStackTrace();
}
return returnFileName;
}
private InsertHistDao insertHistDao;
@Autowired
public void setInsertHistIntoDb(InsertHistDao insertHistDao) {
this.insertHistDao = insertHistDao;
}
@Override
public void getDataFromCsv(File unZipFile,String sym) {
String[] csvLine=null;
try
{
CSVReader csvReader=new CSVReader(new FileReader(unZipFile));
while((csvLine=csvReader.readNext())!=null)
{
System.out.println("data :"+Arrays.toString(csvLine));
insertHistDao.insertHistIntoDb(csvLine,sym);
rowsInserted++;
System.out.println("Rows inserted:"+rowsInserted);
}
csvReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這是我想要做單元測試的類。
@GostCat感謝您對我的代碼的反饋。這非常有幫助。但我沒有懷疑。 1.從您的反饋意見中的第一點開始,您指定我正在使用新的關鍵字及其不良做法。是否我們根本不應該使用'新'關鍵字來更好地控制代碼?因此,如果要爲ex創建一個新的File對象,我們是否應該使用依賴注入來創建它? 2.你能否藉助一些例子來說明你在第二點中指定的'分解成更小的東西'。 –