Java 我如何在另一堂课上测试我的方法?
大约5周前,我才开始学习计算机科学(Java),但在创建方法方面仍然有困难。我被指派创建一个NFL统计类,然后创建一个显示计算的方法。一切都很顺利,直到我在测试课上调用我的方法。这里好像少了什么 NFLPlayer类(方法中包含): 现在是我的测试类,在这里我无法显示我的计算结果Java 我如何在另一堂课上测试我的方法?,java,methods,call,instance-variables,Java,Methods,Call,Instance Variables,大约5周前,我才开始学习计算机科学(Java),但在创建方法方面仍然有困难。我被指派创建一个NFL统计类,然后创建一个显示计算的方法。一切都很顺利,直到我在测试课上调用我的方法。这里好像少了什么 NFLPlayer类(方法中包含): 现在是我的测试类,在这里我无法显示我的计算结果 class MyTest { public static void main(String[] args) { NFLPlayer playerStats = new NFLPlayer(); //
class MyTest {
public static void main(String[] args) {
NFLPlayer playerStats = new NFLPlayer();
//Player1 finding quarterback rating
int touchdowns = 2;
int passingAttempts = 44;
int passingYards = 285;
int interceptions = 1;
int completedPasses = 35;
// Call QB rating method
playerStats.QBRating(touchdowns, passingAttempts, completedPasses,
passingYards, interceptions);
System.out.println(QBRating);
}
}您不应在SOP中调用方法名称,而应调用System.out.println(playerStats.QBRating(触地、传球尝试、完成传球、, 通行场、拦截); 或者重写类中的toString()方法,并将方法调用分配给局部变量并打印值。
还可以使用一些框架(Junit),而不是编写存根,而不是向方法传递太多的
int
参数(很容易混淆),您可以为每个值提供您的NFLPlayer类私有字段:
public class NFLPlayer {
private final String name;
private int touchdowns;
private int passingAttempts;
private int completedPasses;
private int passingYards;
private int interceptions;
public NFLPlayer(String name) {
this.name = name;
}
// Method names start with a lower case character in Java
// The name should usually be an imperative 'do something' not a noun ('something')
// although there are exceptions to this rule (for instance in fluent APIs)
public double calculateQbRating() {
double a = (completedPasses / passingAttempts - 0.3) * 5.0;
double b = (passingYards / passingAttempts - 3.0) * 0.25;
// an AritmeticException will occur if passingAttempts is zero
double c = (touchdowns / passingAttempts) * 25.0;
double d = 2.375 - (interceptions / passingAttempts * 25.0);
return ((a + b + c + d) / 6.0) * 100.0;
}
public String getName() {
return name;
}
// setter for the touchdowns field
public void setTouchdowns(int value) {
touchdowns = value;
}
// TODO: add other setters for each private field
@Override
public String toString() {
return String.format("Player %s has QB rating %s", name, calculateQbRating());
}
}
您的应用程序(这不是测试):
使用JUnit框架对NFLPlayer类进行的测试(默认情况下,JUnit包含在IDE中):
这个测试类应该放在您的测试源目录中(通常在yourproject/src/test/yourpackage/
中)。它需要一些导入,这些导入应该可以在IDE中轻松解析,因为JUnit在默认情况下通常是可用的
要运行测试,请右键单击它并选择类似“运行测试”、“测试文件”之类的内容,具体取决于您使用的IDE(IDE是开发工具,如Eclipse、NetBeans或IntelliJ)。您应该看到一些测试输出,指示测试是否成功(绿色)或是否有失败(红色)。有这样的测试是有用的,因为它迫使您考虑您的设计并编写更好的代码。(可测试代码通常比难以测试的代码好),因为它会在新的更改导致现有代码出现错误时发出警告(回归)
编辑:
要创建两个具有不同统计信息的玩家,您必须创建两个实例(我添加了一个name
字段,以便我们更容易区分玩家):
并给他们各自提供统计数据:
player1.setTouchdowns(2);
player1.setPassingAttempts(4);
player1.setPassingYards(6);
player1.setInterceptions(8);
player1.setCompletedPasses(10);
player2.setTouchdowns(1);
player2.setPassingAttempts(3);
player2.setPassingYards(5);
player2.setInterceptions(7);
player2.setCompletedPasses(9);
您甚至可以创建一个玩家列表:
List<NFLPlayer> players = new ArrayList<>();
players.add(player1);
players.add(player2);
您的QBRating方法返回double,只需在double中收集结果,然后使用sysout@ShekharKhairnar你是说我所做的只是收集一个结果,还是我需要收集一个结果?你需要收集结果,这就是我在评论中提到的,例如,double result=你的行调用QBRating方法的代码你知道吗您在QBRating中使用整数分割?即使有3人无法正确投票,但这并不意味着这是一个好问题。请读一个好问题。没有问题描述的问题没有帮助。感谢您的快速反馈。我对这一切都不熟悉,所以非常感谢。什么是(Junit)框架?当你说override to string时,你的意思是在我的NFLPlayer类中创建一个string方法吗?因为我在网上做所有事情,这有助于我更好地理解如何设置一个完整的程序。我正在为最终会有多个不同统计数据的玩家创建框架。通过使用“设置触地(2);”等。这只是个测试?或者我仍然可以为不同的球员应用不同的统计数据吗?“球员1.设置触地得分(2)”将球员1的触地得分设置为2。您可以为不同的玩家设置不同的值,也可以更改玩家的值。
public class NFLPlayerTest {
// instance of the class-under-test
private NFLPlayer instance;
// set up method executed before each test case is run
@Before
public void setUp() {
instance = new NFLPlayer();
}
@Test
public void testCalculateQbRatingHappy() {
// SETUP
instance.setTouchdowns(2);
instance.setPassingAttempts(44);
instance.setPassingYards(285);
instance.setInterceptions(1);
instance.setCompletedPasses(35);
// CALL
double result = playerStats.calculateQbRating();
// VERIFY
// assuming here the correct result is 42.41, I really don't know
assertEquals(42.41, result);
}
@Test
public void testCalculateQbRatingZeroPassingAttempts() {
// SETUP
// passingAttempts=0 is not handled gracefully by your logic (it causes an ArithmeticException )
// you will probably want to fix this
instance.setPassingAttempts(0);
// CALL
double result = playerStats.calculateQbRating();
// VERIFY
// assuming here that you will return 0 when passingAttempts=0
assertEquals(0, result);
}
}
NFLPlayer player1 = new NFLPlayer("adams");
NFLPlayer player2 = new NFLPlayer("jones");
player1.setTouchdowns(2);
player1.setPassingAttempts(4);
player1.setPassingYards(6);
player1.setInterceptions(8);
player1.setCompletedPasses(10);
player2.setTouchdowns(1);
player2.setPassingAttempts(3);
player2.setPassingYards(5);
player2.setInterceptions(7);
player2.setCompletedPasses(9);
List<NFLPlayer> players = new ArrayList<>();
players.add(player1);
players.add(player2);
for(NFLPlayer player : players) {
// this uses the `toString` method I added in NFLPlayer
System.out.println(player);
}