我正在运行这段代码,并意识到该getAllParameters()方法由于某种原因运行了两次。由于静态字段enumMap是在该方法之外初始化的,因此它会被填充两次,这会导致重复元素并使我正在运行的测试失败。
我认为enumMap在方法内部初始化可以解决问题,因为当该方法第二次运行时地图确实会重置。
尽管这解决了问题,但我想知道为什么在运行 Maven Test 时会发生这种情况?我尝试了参数的数量,认为这可能会影响方法运行的次数,但它似乎只运行了两次。
@RunWith(Parameterized.class)
public class MyTest {
private static Map<String, List<Class<? extends LocalizedJsonEnum>>> enumMap = new HashMap<>();
@Parameter
@SuppressWarnings({"WeakerAccess", "unused"})
public Class<? extends LocalizedJsonEnum> currentEnum;
@Parameter(value = 1)
@SuppressWarnings({"WeakerAccess", "unused"})
public String currentClassName;
/**
* Generate a list of all the errors to run our test against.
*
* @return the list
*/
@Parameters(name = "{1}.class")
public static Collection<Object[]> getAllParameters() throws Exception {
Collection<Object[]> parameters = new LinkedList<>();
Reflections reflections …Run Code Online (Sandbox Code Playgroud)