2017-04-21 11 views
0

This is what I intend to do境界値分析のためのJunitテストケースを設計する際に問題が発生しています。最初のクラスは、階層に基づいてロジックソートを実行するクラスです。 assertEqualsが使用されているとき、プログラムは正しく動作しません。以前のassertEqualsのやっていたことを繰り返し続けました。したがって、最終的には、それ以上の評価は役に立たないとみなされる。Javaの階層アルゴリズムはどのように機能しますか?

public class AssignCharges { 
    RandomGeneratorClass rgc = new RandomGeneratorClass(); 

    // To test for cases less than 0 
    public double getCharges(int distance, int weight) { 
    // declaring the variable to be fit into the given situation 
    double charges = 0; 

    if(weight < 0 || distance < 0) 
     throw new IllegalArgumentException("Values cannot be negative."); 

    // Testing for valid first boundary values 
    // @ <300g, <10km, RM 5 
    else if(weight > 0 && weight < 300){ 
     charges = 5; 
    } 
    // Testing for valid second boundary values @ 300-1000g 
    else if(weight >= 300 && weight < 1000){ 
    // @ distance < 10km, RM 8 
     if(distance > 0 && distance < 10) 
      charges = 8; 
    // @ 10 < distance < 30, RM 10 
     else if(distance >= 10 && distance < 30) 
      charges = 10; 
    // @ distance >= 30, RM 20 
     else 
      charges = 20; 
    } 
    // Testing for valid third boundary values @ 1001-3000 g 
    else if(weight >= 1000 && weight <3000){ 
    // @ < 10km, RM 8 
     if(distance > 0 && distance < 10) 
      charges = 8; 
    // @ 10 < distance < 30, RM 12 
     else if(distance >= 10 && distance < 30) 
      charges = 12; 
    // @ distance > 30, RM 30 
     else 
      charges = 30; 
    } 

    // Testing for valid fourth boundary values @ 3001-5000g 
    else if(weight >= 3000 && weight < 5000){ 
    // @ < 10km, RM 10 
     if(distance > 0 && distance < 10) 
      charges = 10; 
    // @ 10 < distance < 30, RM 15 
     else if(distance >= 10 && distance < 30) 
      charges = 15; 
    // @ distance > 30, RM 40 
     else 
      charges = 40; 
    } 

    // Testing for valid fifth boundary values @ >5000g 
    else if(weight >= 5000){ 
    // @ < 10km, RM 15 
     if(distance > 0 && distance < 10) 
      charges = 15; 
    // @ 10 < distance < 30, RM 20 
     else if(distance >= 10 && distance < 30) 
      charges = 20; 
    // @ distance > 30, RM 50 
     else 
      charges = 50; 
    } 

    // Testing for invalid negative boundary values 
    // The else is sufficient as the only invalid values = negative values 
    else if(weight == 0 && distance == 0) 
     charges = 0; 

    // replies the amount of corresponding charges 
    return charges; 
} 

これはJUnitのコードになります:

public class AssignChargesTest { 
    @Test 
    public void testAssignWeightDistanceInvalidBoundary(){ 
     AssignCharges ac = new AssignCharges(); 
     double charges; 

     // invalid boundary 
     charges = ac.getCharges(0, 0); 
     assertEquals(0, charges, 0); 
    } 

    @Test 
    public void testAssignWeightDistanceFirstBoundary(){ 
     AssignCharges ac = new AssignCharges(); 
     double charges; 

     // first boundary 
     charges = ac.getCharges(299, 5); 
     assertEquals(5, charges, 0); 
    } 

    @Test 
    public void testAssignWeightDistanceSecondBoundary(){ 
     AssignCharges ac = new AssignCharges(); 
     double charges; 

     // second boundary 
     charges = ac.getCharges(999, 5); 
     assertEquals(8, charges, 0); 
     charges = ac.getCharges(999, 20); 
     assertEquals(10, charges, 0); 
     charges = ac.getCharges(999, 40); 
     assertEquals(20, charges, 0); 
    } 

    @Test 
    public void testAssignWeightDistanceThirdBoundary(){ 
     AssignCharges ac = new AssignCharges(); 
     double charges; 

     // third boundary 
     charges = ac.getCharges(2999, 5); 
     assertEquals(8, charges, 0); 
     charges = ac.getCharges(2999, 20); 
     assertEquals(12, charges, 0); 
     charges = ac.getCharges(2999, 40); 
     assertEquals(30, charges, 0); 
    } 

    @Test 
    public void testAssignWeightDistanceFourthBoundary(){ 
     AssignCharges ac = new AssignCharges(); 
     double charges; 

     // fourth boundary 
     charges = ac.getCharges(4999, 5); 
     assertEquals(10, charges, 0); 
     charges = ac.getCharges(4999, 20); 
     assertEquals(15, charges, 0); 
     charges = ac.getCharges(4999, 40); 
     assertEquals(40, charges, 0); 
    } 

    @Test 
    public void testAssignWeightDistanceFifthBoundary(){ 
     AssignCharges ac = new AssignCharges(); 
     double charges; 

     // fifth boundary 
     charges = ac.getCharges(5001, 5); 
     assertEquals(15, charges, 0); 
     charges = ac.getCharges(5001, 20); 
     assertEquals(20, charges, 0); 
     charges = ac.getCharges(5001, 40); 
     assertEquals(50, charges, 0); 
    } 


    @Test(expected = IllegalArgumentException.class) 
    public void testIllegalArgumentException(){ 
     AssignCharges ac = new AssignCharges(); 
     double charges; 

     // invalid arguments list in terms of negative values: 

     // below boundary @ negative weight, negative distance 
     charges = ac.getCharges(-5, -5); 
     // positive weight, negative distance 
     charges = ac.getCharges(299, -5); 
     charges = ac.getCharges(999, -5); 
     charges = ac.getCharges(2999, -5); 
     charges = ac.getCharges(4999, -5); 
     charges = ac.getCharges(5001, -5); 
     // negative weight, positive distance 
     charges = ac.getCharges(-1, 2); 
     charges = ac.getCharges(-2, 12); 
     charges = ac.getCharges(-3, 22); 
     charges = ac.getCharges(-4, 32); 
    } 
} 
+3

最小限の[mcve]強調を作成してください。 –

+0

これは私がやろうとしていることです:http://imgur.com/a/dC1IL –

答えて

0

JUnitのは、Java以外の何ものでもありません。 1つのJava文が突然完了すると(例えば、例外を投げる)、後続の文は実行されない。あなたはこのような一連の文がある場合は

は:

charges = ac.getCharges(-5, -5); 
charges = ac.getCharges(-5, -5); 
charges = ac.getCharges(-5, -5); 
// ... 

(私は彼らが私はちょうど貼り付け+コピーすべて同じだ知っている)

を最初の1に障害が発生したら、他の人は冗長です。

あなたは実行が失敗を超えて進行させるためには、try/catchブロックで呼び出しをラップするような何か実行する必要があります。

try { 
    ac.getCharges(-5, -5); 
    fail(); 
} catch (IllegalArgumentException expected) { 
} 
try { 
    ac.getCharges(-5, -5); 
    fail(); 
} catch (IllegalArgumentException expected) { 
} 
// ... 

ので、実行は継続しますが。

これは明らかに非常に冗長です。代わりに、このようなメソッドを書いて考えてみます。

void assertGetChargesFails(int a, int b) { 
    try { 
    ac.getCharges(a, b); 
    fail(); 
    } catch (IllegalArgumentException expected) { 
    } 
} 

、その後のように呼び出す:あなたはJUnitの4.15以上とJava 8を使用している場合

assertGetChargesFails(-5, -5); 
assertGetChargesFails(-5, -5); 
assertGetChargesFails(-5, -5); 

、もっときれいにこれを行いassertThrows methodがあります:

例外がスローされていない場合 assertThrowsラインのそれぞれは失敗します
assertThrows(IllegalArgumentException.class,() -> ac.getChanges(-5, -5)); 
assertThrows(IllegalArgumentException.class,() -> ac.getChanges(-5, -5)); 

+0

ありがとう、今どこで問題のデバッグを開始するのか知っています。私はそれを解決し始めるでしょう。 –

関連する問題