Domanda

Posso avere più di un metodo con @Parameters nella classe di test junit che è in esecuzione con la classe con parametri?

@RunWith(value = Parameterized.class)
public class JunitTest6 {

 private String str;

 public JunitTest6(String region, String coverageKind,
        String majorClass, Integer vehicleAge, BigDecimal factor) {
    this.str = region;
 }

  @Parameters
 public static Collection<Object[]> data1() {
   Object[][] data = {{some data}}

   return Arrays.asList(data);
 }

 @Test
 public void pushTest() {
   System.out.println("Parameterized str is : " + str);
   str = null;
 }

 @Parameters
 public static Collection<Object[]> data() {
   Object[][] data = {{some other data}}
   return Arrays.asList(data);
 }

 @Test
 public void pullTest() {
   System.out.println("Parameterized new str is  : " + str);
   str = null;
 }
}
È stato utile?

Soluzione

Puoi utilizzare il Theories (cerca le parole teorie su quel link) per passare parametri diversi a metodi diversi.

Altri suggerimenti

Probabilmente il metodo data1 , ma nessuna garanzia di ciò, userà quello che la JVM fornisce per prima a junit4.

Ecco il codice pertinente di junit:

private FrameworkMethod getParametersMethod(TestClass testClass) throws Exception {
    List<FrameworkMethod> methods= testClass.getAnnotatedMethods(Parameters.class);
    for (FrameworkMethod each : methods) {
        int modifiers= each.getMethod().getModifiers();
            if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
                return each;
    }

    throw new Exception("No public static parameters method on class " + testClass.getName());
}

Quindi verrà utilizzato il primo metodo pubblico, annotato statico che trova, ma può trovarli in qualsiasi ordine.

Perché hai scritto il tuo test in quel modo? Dovresti avere solo un metodo @Parameters .

Non è designato per avere più di un metodo di dati. Puoi vederlo in risposta di skaffman .

Perché non viene fornito per implementare due metodi di dati?
La risposta potrebbe essere: accoppiamento.

È troppo complesso dividere questo test in due casi di test? Saresti in grado di introdurre una piccola eredità e condividere metodi comuni. Con due test-case potresti fornire due metodi di dati separati e testare le tue cose molto bene.

Spero che sia d'aiuto.

È possibile creare classi interne per ogni set di metodi che operano sugli stessi parametri. Ad esempio:

public class JunitTest6 {

 @RunWith(value = Parameterized.class)
 public static class PushTest{
  private String str;
  public PushTest(String region) {
   this.str = region;
  }

  @Parameters
  public static Collection<Object[]> data() {
   Object[][] data = {{some data}}

   return Arrays.asList(data);
  }

  @Test
  public void pushTest() {
   System.out.println("Parameterized str is : " + str);
   str = null;
  }
 }

 @RunWith(value = Parameterized.class)
 public static class PullTest{
  private String str;
  public PullTest(String region) {
   this.str = region;
  }

  @Parameters
  public static Collection<Object[]> data() {
   Object[][] data = {{some other data}}
   return Arrays.asList(data);
  }

  @Test
  public void pullTest() {
   System.out.println("Parameterized new str is  : " + str);
   str = null;
  }
 }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top