Junit4

2016-10-21 5 views
0

私はこれを実行している、私がテストメソッドで複数の条件をテストできるようにするパラメータ化テストとして実行しようとしているJUnitテスト、下に持っているしかしJunit4

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.junit.runners.Parameterized; 

import java.util.Arrays; 
import java.util.List; 

import static org.junit.Assert.assertEquals;  
@RunWith(Parameterized.class) 
    public class ProductCategoryTest { 


     private boolean isAggregate; 
     private ProductCategory category; 

     public ProductCategoryTest(boolean isAggregate, ProductCategory category) { 
      this.isAggregate = isAggregate; 
      this.category = category; 
     } 

     @Parameterized.Parameters 
     public static List<Object[]> categoryList() { 
      return Arrays.asList(new Object[][]{ 
        {true, new ProductCategory("A", "Laundry", ProductCategory.Type.Aggregate)}, 
        {false, new ProductCategory("B", "Dryer", ProductCategory.Type.Individual)}, 
      }); 
     } 

     @Test 
     public void isAggregateProductCategory() throws Exception { 
      assertEquals(isAggregate, category.isAggregateProductCategory()); 
     } 

    } 

私が手例外:

java.lang.Exception: No tests found matching categoryList with any parameter from [email protected] 

    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:96) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
+0

が直接使用されています。それを試しましたか? (http://junit.sourceforge.net/javadoc/org/junit/runners/Parameterized.html) –

答えて

1

自分のコードを自分のPCで試しましたが、それは自分のPC上で正常に動作します。 あなたの環境で使用しているライブラリやJUnitランナーに問題があると思われます。

あなた自身のテストランナーを試してみてください。それを行う方法の例を次に示します:APIの `@ Parameters`アノテーションの中で、

import org.junit.Test; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.RunWith; 
import org.junit.runner.notification.Failure; 
import org.junit.runners.Parameterized; 
import java.util.Arrays; 
import java.util.List; 
import org.junit.runner.Result; 
import static org.junit.Assert.assertEquals; 

class ProductCategory { 
    enum Type { 
     Aggregate, Individual 
    }; 

    public ProductCategory(String a, String b, Type t) { 

    } 
} 

@RunWith(Parameterized.class) 
    public class ProductCategoryTest { 


     private boolean isAggregate; 
     private ProductCategory category; 

     public ProductCategoryTest(boolean isAggregate, ProductCategory category) { 
      this.isAggregate = isAggregate; 
      this.category = category; 
     } 

     @Parameterized.Parameters 
     public static List<Object[]> categoryList() { 
      return Arrays.asList(new Object[][]{ 
        {true, new ProductCategory("A", "Laundry", ProductCategory.Type.Aggregate)}, 
        {false, new ProductCategory("B", "Dryer", ProductCategory.Type.Individual)}, 
      }); 
     } 

     @Test 
     public void isAggregateProductCategory() throws Exception { 
      //assertEquals(isAggregate, category.isAggregateProductCategory()); 
     } 


     public static void main(String[] args) { 
      Result result = JUnitCore.runClasses(ProductCategoryTest.class); 

      for (Failure failure : result.getFailures()) { 
       System.out.println(failure.toString()); 
      } 

      System.out.println(result.wasSuccessful()); 
     } 
    } 
+0

私はJunitを設定するSpringブートスターターテストを持っています –

+0

テストは問題なく実行されます。 ? –

+0

となり、テストは今すぐ通過します!私はあなたの答えに基づいて投稿した主な方法を削除するだけです。 –

関連する問題