001package edu.pdx.cs.joy; 002 003import java.util.List; 004 005/** 006 * Throws when the <code>main</code> method of a class with mutable <code>static</code> 007 * fields is invoked with {@link InvokeMainTestCase#invokeMain(Class, String...)}. 008 * 009 * Main classes with mutable static fields have proven to be problematic because the 010 * values of the mutable fields are not reset in between tests. As a result, the values 011 * of these fields from previous tests might stick around. This has proven to be very 012 * confusing. 013 * 014 * @see InvokeMainTestCase#invokeMainAllowingMutableStaticFields(Class, String...) 015 */ 016public class MainClassContainsMutableStaticFields extends RuntimeException { 017 018 private final Iterable<String> fieldNames; 019 020 public MainClassContainsMutableStaticFields(String className, List<String> fieldNames) { 021 super(getMessage(className, fieldNames)); 022 this.fieldNames = fieldNames; 023 } 024 025 private static String getMessage(String className, List<String> fieldNames) { 026 return "Main class \"" + 027 className + 028 "\" contains " + 029 fieldNames.size() + 030 " non-final static fields: " + 031 String.join(", ", fieldNames) + 032 ". You probably don't want to store data in mutable static fields. " + 033 "It might cause confusing behavior with your integration tests."; 034 } 035 036 public Iterable<String> getNamesOfMutableStaticFields() { 037 return this.fieldNames; 038 } 039}