在带有参数的测试中未找到 JUnit 测试
No JUnit test found in test with parameters
我刚刚开始学习 JUnit 测试,我已经获得了使用参数进行测试的示例,因此我正在应用它以及 Daniel Mayer's Our Craft and Mkyong.
等站点的文档和示例
我被要求用参数化 class 测试 fill() 方法。这是原文class:
package FuelTankTestPractice;
/**
* FuelTank is the class which represents the fuel tank of a car.
* A FuelTank object encapsulates the state information needed for describing the state of the tank:
* <ul>
* <li> tankMax capacity of the tank
* <li> tankLevel fuel level of the tank
* </ul>
*
* class invariant 0.0 <= tankLevel <= tankMax
*
* @author UC3M MOOC Team
*
*/
public class FuelTank {
private double tankMax;
private double tankLevel;
/**
* FuelTank is a constructor of the class.
*
* <hr>
* <br> precondition tankMax > 0.0 and 0.0 <= tankLevel <= getTankMax()
* <br> postcondition tankMax > 0.0 and 0.0 <= tankLevel <= getTankMax()
* <hr>
*
* @param tankMax is the amount of fuel (measured in liters) that the tank can hold
* @param tankLevel is the amount of fuel (measured in liters) that the tank will have initially
*
*/
FuelTank(double tankMax, double tankLevel) {
this.tankMax = tankMax;
this.tankLevel = tankLevel;
}
/**
* getTankLevel is an accessor method
*
* @return the amount of fuel in the tank
*/
public double getTankLevel(){
return tankLevel;
}
/**
* getTankMax is an accessor method
*
* @return the capacity (in liters) of the tank
*/
public double getTankMax(){
return tankMax;
}
/**
* isEmpty gives a status report
*
* @return <code>true</code> if the tank is empty
* <code>false</code> otherwise.
*/
public boolean isEmpty(){
return tankLevel == 0;
}
/**
* isFull gives a status report
*
* @return <code>true</code> if the tank is full
* <code>false</code> otherwise.
*/
public boolean isFull(){
return tankLevel == tankMax;
}
/**
* fill is a mutator method that adds fuel to the tank
*
* <hr>
* <br> precondition 0.0 < amount <= getTankMax() - getTankLevel()
* <br> postcondition not empty
* <br> postcondition tankLevel > tankLevel_initial
* <hr>
*
* @param amount the quantity of fuel to add
*
*/
public void fill(double amount){
tankLevel = tankLevel + amount;
}
/**
* consume is a mutator that consumes amount of fuel
*
* @param amount the amount of fuel to consume
*
*/
public void consume(double amount){
tankLevel = tankLevel - amount;
}
}
首先,我必须创建一个简单的测试 class,该测试没有问题:
package FuelTankTestPractice;
/**
* Tests for class FuelTank.
*
* All tests in the folder "test" are executed
* when the "Test" action is invoked.
*
*/
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
public class FuelTankTest {
FuelTank tank = null;
@Before
public void setUp() throws Exception {
tank = new FuelTank(60.0,10.0);
}
@Test
public void testGetTankLevel() {
tank.getTankLevel();
assertTrue(tank.getTankLevel()==0.0);
}
@Test
public void testGetTankMax() {
tank.getTankMax();
assertTrue(tank.getTankMax()==60.0);
}
@Test
public void testIsEmpty() {
tank.isEmpty();
assertTrue(!tank.isEmpty());
}
@Test
public void testHalfFullTank() {
assertTrue(tank.getTankMax()/2==30.0);
}
}
但是现在这是我带参数的测试class(想法是测试fill()方法:
package FuelTankTestPractice;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class FillingTest extends FuelTankTest{
private double amount;
private double result;
//constructor
public FillingTest (double amount, double result) {
this.amount = amount;
this.result = result;
}
FuelTank tank = new FuelTank(60, 10);
//Declares parameters here
public static Collection<Object[]> fillAmounts(){
Object[][] amounts = new Object[][]{
{10.0,20.0},
{15.0,35.0},
{20.0,30.0},
{35.0,45.0}};
return Arrays.asList(amounts);
}
@Test
public void TestFill() {
tank.fill(amount);
assertTrue(amount + tank.getTankLevel() == result);
}
}
主要的 class 和方法非常简单,但如果有帮助我会添加它:
public class Main {
public static void main(String[] args) {
// create the tank
FuelTank tank = new FuelTank(40.0,0.0);
System.out.print("The tank with capacity " + tank.getTankMax() + " liters has been created. ");
System.out.println(" Its initial fuel level is " + tank.getTankLevel() + " liters.");
}
}
我做了很多更改以适应示例,但其中大多数不考虑另一个测试 class(我最终需要按照指示创建 JUnit 测试套件)。
有时它会抛出:
java.lang.Exception: No public static parameters method on class FuelTankTestPractice.FillingTest
at org.junit.runners.Parameterized.getParametersMethod(Parameterized.java:299)
at org.junit.runners.Parameterized.<init>(Parameterized.java:246)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
希望所有这些信息对您有所帮助。这不是要解决我的 "homework",而是要学习。在考虑作为菜鸟问题之前,请问我任何你需要的东西,我已经做了我能做的所有研究,但我相信例子不符合我的问题,或者我可能不知道有什么不同。
您声明您的测试 class 应用作 @RunWith(Parameterized.class)
也就是说它提供了一个带有@Parameters
注解的方法。
因此:将此注释添加到您的方法中 fillAmounts()
,您应该会很好。
该错误消息基本上是在告诉您运行器找不到完成其工作所需的方法。
这是通过测试的最终工作代码:
package FuelTankTestPractice;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
//Part 1: Runwith
@RunWith(Parameterized.class)
public class FillingTest {
private double amount;
private double result;
//Part 2: Constructor
public FillingTest (double amount, double result) {
this.amount = amount;
this.result = result;
}
//Part 3: Declares parameters here
@Parameters
public static Collection<Object[]> fillAmounts(){
Object[][] amounts = new Object[][]{
{10.0,20.0},
{15.0,25.0},
{20.0,30.0},
{35.0,45.0}};
return Arrays.asList(amounts);
}
// Part 4: Test method
FuelTank tank = new FuelTank(60.0,10.0);
@Test
public void TestFill() {
tank.fill(amount);
assertEquals(result, tank.getTankLevel(), 0.0001);
// Or assertTrue(tank.getTankLevel() == result);
}
}
与问题代码相比,第 3 部分缺少 @Parameters
行。
此外,第 4 部分中的 assertTrue() 行是错误的,因为 tank.fill(amount);
已经将 "amount" 添加到 TankLevel。
为了课程的目的,我使用了 assertEquals 而不是 assertTrue(这让我寻找 "delta" 的含义)。
我刚刚开始学习 JUnit 测试,我已经获得了使用参数进行测试的示例,因此我正在应用它以及 Daniel Mayer's Our Craft and Mkyong.
等站点的文档和示例我被要求用参数化 class 测试 fill() 方法。这是原文class:
package FuelTankTestPractice;
/**
* FuelTank is the class which represents the fuel tank of a car.
* A FuelTank object encapsulates the state information needed for describing the state of the tank:
* <ul>
* <li> tankMax capacity of the tank
* <li> tankLevel fuel level of the tank
* </ul>
*
* class invariant 0.0 <= tankLevel <= tankMax
*
* @author UC3M MOOC Team
*
*/
public class FuelTank {
private double tankMax;
private double tankLevel;
/**
* FuelTank is a constructor of the class.
*
* <hr>
* <br> precondition tankMax > 0.0 and 0.0 <= tankLevel <= getTankMax()
* <br> postcondition tankMax > 0.0 and 0.0 <= tankLevel <= getTankMax()
* <hr>
*
* @param tankMax is the amount of fuel (measured in liters) that the tank can hold
* @param tankLevel is the amount of fuel (measured in liters) that the tank will have initially
*
*/
FuelTank(double tankMax, double tankLevel) {
this.tankMax = tankMax;
this.tankLevel = tankLevel;
}
/**
* getTankLevel is an accessor method
*
* @return the amount of fuel in the tank
*/
public double getTankLevel(){
return tankLevel;
}
/**
* getTankMax is an accessor method
*
* @return the capacity (in liters) of the tank
*/
public double getTankMax(){
return tankMax;
}
/**
* isEmpty gives a status report
*
* @return <code>true</code> if the tank is empty
* <code>false</code> otherwise.
*/
public boolean isEmpty(){
return tankLevel == 0;
}
/**
* isFull gives a status report
*
* @return <code>true</code> if the tank is full
* <code>false</code> otherwise.
*/
public boolean isFull(){
return tankLevel == tankMax;
}
/**
* fill is a mutator method that adds fuel to the tank
*
* <hr>
* <br> precondition 0.0 < amount <= getTankMax() - getTankLevel()
* <br> postcondition not empty
* <br> postcondition tankLevel > tankLevel_initial
* <hr>
*
* @param amount the quantity of fuel to add
*
*/
public void fill(double amount){
tankLevel = tankLevel + amount;
}
/**
* consume is a mutator that consumes amount of fuel
*
* @param amount the amount of fuel to consume
*
*/
public void consume(double amount){
tankLevel = tankLevel - amount;
}
}
首先,我必须创建一个简单的测试 class,该测试没有问题:
package FuelTankTestPractice;
/**
* Tests for class FuelTank.
*
* All tests in the folder "test" are executed
* when the "Test" action is invoked.
*
*/
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
public class FuelTankTest {
FuelTank tank = null;
@Before
public void setUp() throws Exception {
tank = new FuelTank(60.0,10.0);
}
@Test
public void testGetTankLevel() {
tank.getTankLevel();
assertTrue(tank.getTankLevel()==0.0);
}
@Test
public void testGetTankMax() {
tank.getTankMax();
assertTrue(tank.getTankMax()==60.0);
}
@Test
public void testIsEmpty() {
tank.isEmpty();
assertTrue(!tank.isEmpty());
}
@Test
public void testHalfFullTank() {
assertTrue(tank.getTankMax()/2==30.0);
}
}
但是现在这是我带参数的测试class(想法是测试fill()方法:
package FuelTankTestPractice;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class FillingTest extends FuelTankTest{
private double amount;
private double result;
//constructor
public FillingTest (double amount, double result) {
this.amount = amount;
this.result = result;
}
FuelTank tank = new FuelTank(60, 10);
//Declares parameters here
public static Collection<Object[]> fillAmounts(){
Object[][] amounts = new Object[][]{
{10.0,20.0},
{15.0,35.0},
{20.0,30.0},
{35.0,45.0}};
return Arrays.asList(amounts);
}
@Test
public void TestFill() {
tank.fill(amount);
assertTrue(amount + tank.getTankLevel() == result);
}
}
主要的 class 和方法非常简单,但如果有帮助我会添加它:
public class Main {
public static void main(String[] args) {
// create the tank
FuelTank tank = new FuelTank(40.0,0.0);
System.out.print("The tank with capacity " + tank.getTankMax() + " liters has been created. ");
System.out.println(" Its initial fuel level is " + tank.getTankLevel() + " liters.");
}
}
我做了很多更改以适应示例,但其中大多数不考虑另一个测试 class(我最终需要按照指示创建 JUnit 测试套件)。 有时它会抛出:
java.lang.Exception: No public static parameters method on class FuelTankTestPractice.FillingTest
at org.junit.runners.Parameterized.getParametersMethod(Parameterized.java:299)
at org.junit.runners.Parameterized.<init>(Parameterized.java:246)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
希望所有这些信息对您有所帮助。这不是要解决我的 "homework",而是要学习。在考虑作为菜鸟问题之前,请问我任何你需要的东西,我已经做了我能做的所有研究,但我相信例子不符合我的问题,或者我可能不知道有什么不同。
您声明您的测试 class 应用作 @RunWith(Parameterized.class)
也就是说它提供了一个带有@Parameters
注解的方法。
因此:将此注释添加到您的方法中 fillAmounts()
,您应该会很好。
该错误消息基本上是在告诉您运行器找不到完成其工作所需的方法。
这是通过测试的最终工作代码:
package FuelTankTestPractice;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
//Part 1: Runwith
@RunWith(Parameterized.class)
public class FillingTest {
private double amount;
private double result;
//Part 2: Constructor
public FillingTest (double amount, double result) {
this.amount = amount;
this.result = result;
}
//Part 3: Declares parameters here
@Parameters
public static Collection<Object[]> fillAmounts(){
Object[][] amounts = new Object[][]{
{10.0,20.0},
{15.0,25.0},
{20.0,30.0},
{35.0,45.0}};
return Arrays.asList(amounts);
}
// Part 4: Test method
FuelTank tank = new FuelTank(60.0,10.0);
@Test
public void TestFill() {
tank.fill(amount);
assertEquals(result, tank.getTankLevel(), 0.0001);
// Or assertTrue(tank.getTankLevel() == result);
}
}
与问题代码相比,第 3 部分缺少 @Parameters
行。
此外,第 4 部分中的 assertTrue() 行是错误的,因为 tank.fill(amount);
已经将 "amount" 添加到 TankLevel。
为了课程的目的,我使用了 assertEquals 而不是 assertTrue(这让我寻找 "delta" 的含义)。