0

Assuming, i want use a predefined static final list instance(s) as a holder of some "configuration" for a test. This is a list, so i use static {} block where some values are added to the instances:

public class Config{
        ...
public static final List<Object[]> config = new ArrayList<>();
static{
//object[] are always pairs her`e
config.add(new Object[] { ... whatever});
config.add(new Object[] { ... whatever});
config.add(new Object[] { ... whatever});
}
}
...
//then the test class:
@RunWith(Parameterized.class)
public class GeneralTemplate{

...

    @Parameters(name = "{index}: source: {0} target: {1}")
    public static Collection<Object[]> config() {
        return Config.config;
    }
}

I will use then this instances in the JUnit test class. I guess, because of this definitions + annotations the test runs with the mistake "no tests have been found"?

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
J J
  • 146
  • 2
  • 11
  • I tried your scenario and it worked (with eclipse). It seems that your variable is read before static initialization. This may occur if there is a cyclic dependency between the static initializer and the user of the initialized object. Can you give a complete example that reproduces the problem (with `@Test` and with minimal config/params). – CoronA Jan 17 '16 at 10:07
  • I can not give complete example, it is not a freeware project. An acquaintance said the same: it can depend on the order of static initialization. Means, some JUNIT functions/annotations work before static{add}, and an empty list is read. Can you advice, hot to make it order - independent ? – J J Jan 17 '16 at 13:08

2 Answers2

0

I think @Test annotation is missing from templates. Compiler complaining "no tests have been found" as it in not able to find any test methods with junit 4 @Test annotation in test class defination.

Mohit Sharma
  • 187
  • 1
  • 13
  • Its kinda complicated and weird case, and the method with @Test annotation is inherited from the superclass. **but** The test runs, if i replace the Config.config to some getConfig() method, where the List is created ( like in tutorials about @Parameters) – J J Jan 15 '16 at 21:32
  • static members belong to the class instead of a specific instance. config object is in memory before any class instances. Accessor methods are used to get and set the static values. – Mohit Sharma Jan 15 '16 at 21:47
0

Although I cannot reproduce your problem, following should be tried:

public class Config{
    ...
  public static final List<Object[]> config = createConfig();

  private static List<Object[]> createConfig() {
    List<Object[]> config = new ArrayList<>();
    config.add(new Object[] { ... whatever});
    config.add(new Object[] { ... whatever});
    config.add(new Object[] { ... whatever});
  }
}

or just put this createConfig directly into your method config.

CoronA
  • 7,717
  • 2
  • 26
  • 53