简体   繁体   中英

JUnit integration test with parameterized annotations

In my integration tests I use custom annotations to start part of my application. I definitively need to use these annotations for my tests. Therefore, a typical integration test looks like this:

@Test
@MyAnnotation(a = MyEnum.B, b = someOtherConstant)
public void test() {
}

What I'd like to do is to run the tests for all enums in MyEnum. I thought about using a parameterized tests. But since the value I pass into the annotation must be constant, I think this is not an option. What other options do I have?

I don't think there is a need for an annotation here.

A very simple enum:

package stackoverflow52828976;

public enum MyEnum
{
   A,
   B,
   C;

   public boolean isA()
   {
      return this == A;
   }
}

The test class:

package stackoverflow52828976;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith( Parameterized.class )
public class MyEnumTest
{

   @Parameterized.Parameters
   public static Collection<Object[]> data()
   {
      final List<Object[]> list = new LinkedList<>();

      list.add(new Object[]{ MyEnum.A, true});
      list.add(new Object[]{ MyEnum.B, false});
      list.add(new Object[]{ MyEnum.C, false});

      return list;
   }


   private final MyEnum e;
   private final boolean testResult;


   public MyEnumTest
         ( final MyEnum    anEnum
         , final boolean   aTestResult
         )
   {
      this.e = anEnum;
      this.testResult = aTestResult;
   }


   @Test
   public void testIsA() throws Exception
   {
      assertEquals(testResult, e.isA());
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM