Yöntemi değiştirerek aşağıdaki yöntemi test etmem gerekiyor. Yöntem, bir sunucuya bir POST yöntemi yapar. Ama sunucudan bağımsız bir test durumu yapmam gerekiyor.Java Junit testi HTTP POST isteği
Yerel bir dosyaya yönlendirmeden önce benzer bir yöntemi test ettim. Ama bunun için dosya olarak protokol, localhost olarak hostname ve -1 portu veriyordum.
Sorunum, bu yöntemin bir gönderi yapması ve HttpURLConnection ve wr = new DataOutputStream (conn.getOutputStream()); http yoluyla yerel bir txt dosyasında çalışmaz.
// Yapıcı
public HTTPConnector(String usr, String pwd, String protocol,
String hostname, int port) {
this.usr = usr;
this.pwd = pwd;
this.protocol = protocol;
this.hostname = hostname;
this.port = port;
// connect();
}
// Ben
public String doPost(String reference, String data) throws IOException {
URL url = null;
HttpURLConnection conn = null;
BufferedReader rd = null;
DataOutputStream wr = null;
InputStream is = null;
String line = null;
StringBuffer response = null;
url = new URL(protocol, hostname, port, reference);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic dGVzdDphc2Rm");
conn.setRequestProperty("Content-Type", "application/xml");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
// Send response
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();
// Get response
is = conn.getInputStream();
rd = new BufferedReader(new InputStreamReader(is));
response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
}
// yöntem test etmek gerek yöntem ben
public String doGet(String reference) throws IOException {
connect();
URL url = new URL(protocol, hostname, port, reference);
InputStream content = (InputStream) url.getContent();
BufferedReader xml = new BufferedReader(new InputStreamReader(content));
return xml.readLine();
}
Sahte nesnelere baktınız mı? – buymypies
wel herhangi bir yerel bir yere gitmiyor, bu yüzden nasıl yönlendirileceğini zor buluyorum. yerel bir dosyaya veya sınıfa yönlendirmek için bir yol bulabilirsem, o zaman bir iş yapabilirdim, ancak görebildiğim tek yol yerel bir sunucu/iş parçacığı yapmak. fakat bu, testlerimi sunucuya yönlendirmem, veriyi almak için ihtiyaç duyduğum sunucu ve kendi kendine ihtiyaç duyduğu sunucuyu talep etmek için programlanmalı ve o kadar verimli ve verimli çalışıyor. – Gabain1993