2016-05-25 18 views
2

私はちょうどJUnitテストを習得しており、パラメータを使ったテストの例が与えられていますので、Daniel Mayer's Our CraftMkyongのようなサイトのドキュメントと例を適用しています。パラメータでのテストでJUnitテストが見つかりません

fill()メソッドをパラメータ化されたクラスでテストするように求められます。まず

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 &lt;= tankLevel &lt;= 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 &gt; 0.0 and 0.0 &lt;= tankLevel &lt;= getTankMax() 
    * <br> postcondition tankMax &gt; 0.0 and 0.0 &lt;= tankLevel &lt;= 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 &lt; amount &lt;= getTankMax() - getTankLevel() 
    * <br> postcondition not empty 
    * <br> postcondition tankLevel &gt; 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; 
    } 
} 

私は簡単なテストクラスを作成しなければならなかった、問題なく働いていた:これは、元のクラスです

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); 
    } 

} 

をしかし、今、これは私のテストクラスは、アイデアがテストすることです(パラメータであります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); 
    } 
} 

メインクラスとメソッドは非常に単純ですが、それは場合に役立ちます私はそれを追加します:

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."); 
} 
} 

私は例に合わせて多くの変更を加えましたが、大部分の人は別のテストクラスを検討しませんでした(指示通りに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) 

私は、すべてこの情報がお役に立てば幸いです。私の「宿題」を解決するのではなく、学習することです。喜んで私に質問する前に必要なものを私に尋ねる、私はできるすべての研究を行ったが、私の例は私の質問に合っていないと思う、または多分私は違いを見るのに十分なことを知らない。

答えて

1

これはテストに合格し、最終的な作業コードです:質問コードに比べ

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ラインを欠いています。 また、tank.fill(amount);がすでにTankLevelに "量"を追加しているので、パート4のassertTrue()行が間違っていました。

コースの目的のために、私はassertTrueの代わりにassertEqualsを使用しました(これは「デルタ」の意味を探していました)。

+0

これは私にJUnitについて学び、パラメータを使ってテストするのを助けてくれました(私は午前2時に開始しました。午前5時30分にStackOverflowのヘルプが必要と判断しました。テストコードのすべての部分を再チェックする – Nooblhu

3
はあなたのテストクラスは、それが @Parameters注釈を運ぶ方法を提供することを意味 @RunWith(Parameterized.class)

として使用されるべきであることを宣言している

したがって、この注釈をメソッドfillAmounts()に追加してください。

エラーメッセージは基本的に、ランナーがジョブを実行するために必要な方法を見つけることができないことを伝えています。

関連する問題