我必须为这个类创建JUnit
个测试用例。其中一个测试是测试ShannonsTheorem
类的构造函数。有没有办法测试没有任何参数的构造函数?
另一个名为ShannonsModel
的类也需要对其构造函数进行测试。根据我们提供的UML,在该构造函数上也没有参数。
谢谢!
package network;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ShannonsTheorem {
ShannonsModel model = new ShannonsModel();
Scanner kb = new Scanner(System.in);
/**
* default constructor for ShannonsTheorem class.
*
*/
public ShannonsTheorem()
{
}
/**
* Method to return the value in bandwidth variable.
* @return
*/
public double getBandwidth()
{
return model.getBandwidth();
}
/**
* getSignalToNoise method to return the signalToNoise value in variable signalToNoise
* @return
*/
public double getSignalToNoise()
{
return model.getSignalToNoise();
}
/**
* main method for ShannonsTheorem
* @param args
* while loop to handle user input to continue or end program
*/
public static void main(String[] args)
{
try {
Scanner kb = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
boolean stop = false;
while(!stop) {
ShannonsTheorem ST = new ShannonsTheorem();
System.out.println("Enter bandwidth in hertz:");
ST.setBandwidth(kb.nextDouble());
System.out.println("Enter signalToNoise:");
ST.setSignalToNoise(kb.nextDouble());
System.out.println("Values are:");
System.out.println("Bandwidth");
System.out.println(ST.getBandwidth());
System.out.println("SignalToNoise:");
System.out.println(ST.getSignalToNoise());
System.out.println(ST.maxiumumDataRate());
System.out.println("Press any key to make another calculation. Type N or n to Quit");
String s = scan.nextLine();
if(s.equals("n") || s.equals("N")) {
stop = true;
} // end of if
} // end of while loop
}catch (InputMismatchException e){
System.out.println("Input Exception was caught, restart program");
}catch(NumberFormatException e){
System.out.println("Format Exception was caught, restart program");
}
}
/**
* public method to retrieve the maximum data rate. This method makes a call to the private method
* under the same name.
* @return
*/
public double maxiumumDataRate()
{
// calling to the private method maxiumumDataRate. Storing the return value from said method into variable result
// when this public method is called it will return the result from the private method,
double result = model.maxiumumDataRate();
System.out.print(model.toString());
return result;
}
/**
* setBandwidth method to set the bandwidth value in hertz
* @param h
*/
public void setBandwidth(double h)
{
model.setBandwidth(h);
}
/**
* setSignalToNoise method to set the signalToNoise variable
* @param snr
*/
public void setSignalToNoise(double snr)
{
model.setSignalToNoise(snr);
}
}
答案 0 :(得分:3)
为什么需要测试构造函数?
您可以在不进行任何修改的情况下对其进行测试,默认构造函数具有一些特定字段:
@Test
public void shouldCreateADefaultShannonTheorem() {
ShannonsTheorem shannonsTheorem = new ShannonsTheorem();
Object expectedModel = new ShannonsModel();
assertEquals(expectedModel , shannonsTheorem.model);
Object expectedKb = new Scanner(System.in);
assertEquals(expectedKb , shannonsTheorem.kb);
}
或者您可以在没有任何更改的情况下测试它,默认构造函数会为您提供一些结果:
ShannonsTheorem shannonsTheorem = new ShannonsTheorem();
double expectedbandwith = 0.0;
assertEquals(expectedbandwith , shannonsTheorem.getBandwidth(), 0);
int expectedSignalToNoise = 0;
assertEquals(expectedSignalToNoise , shannonsTheorem.getSignalToNoise(), 0);
int expectedMaximumDataRate = 0;
assertEquals(expectedMaximumDataRate , shannonsTheorem.maxiumumDataRate(), 0);
// ...
这就是为什么它有用来做TDD(先测试):
您的应用应该做什么?写测试。 //这是思考
编写代码。 //不要在这里思考!
重构