我这里有一个简单的程序来将主机名解析为ip地址,反之亦然,并且没有任何想法,只要它们不返回任何值,如何使用junit测试这两个void方法。 我需要有关junit test的帮助,因为我的void方法不会返回任何东西。
/*
* inside this interface we have two function,that
* resolve ip to host-addr and vice versa
*/
public interface DnsFunctions {
public void resolveHostToIp();
public void resolveIpToHost();
}
这是Dns解析器的主要代码,其中在switch实例内调用void方法:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class DnsResolver implements DnsFunctions {
static BufferedReader br;
static DnsFunctions df = new DnsResolver();
static boolean status = true;
public static void main(String[] args) throws NumberFormatException, IOException {
StringBuilder sb = new StringBuilder();
sb.append("Welcome to DNS-resolver page :");
sb.append("\n*select (1) to resolve Hostname to IP-Address");
sb.append("\n*select (2) to get the Host name from inputed IP-Address");
sb.append("\n*select (3) for EXIT");
System.out.println(sb);
/*
* depending on the inputted value from user /1-2 or 3/ the suitable function
* will be called
*/
while (status) {
br = new BufferedReader(new InputStreamReader(System.in));
int inputedValue = Integer.parseInt(br.readLine());
switch (inputedValue) {
case 1:
df.resolveHostToIp();
status = true;
break;
case 2:
df.resolveIpToHost();
status = true;
break;
case 3:
status = false;
System.out.println("GoodBye :)");
break;
}
}
}
@Override
/*
*
* @parameter value
*/
public void resolveHostToIp() {
try {
System.out.println("\n Enter Hostname:");
String hostName = br.readLine();
InetAddress address = InetAddress.getByName(hostName);
System.out.println("Hostname :" + address.getHostName());
System.out.println("IP:" + address.getHostAddress());
} catch (Exception e) {
System.out.println("ERROR :(" + e.getMessage());
}
}
@Override
/*
* @parameter value
*/
public void resolveIpToHost() {
try {
System.out.println("\n Enter IP address");
String ip_add = br.readLine();
InetAddress ia = InetAddress.getByName(ip_add);
System.out.println("IP: " + ip_add);
System.out.println("Host Name: " + ia.getHostName());
} catch (IOException e) {
System.out.println("ERROR :(" + e.getMessage());
}
}
}