Class StaticFieldsInvariantRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class StaticFieldsInvariantRule
    extends java.lang.Object
    implements org.junit.rules.TestRule
    A TestRule that ensures static, reference fields of the suite class (and optionally its superclasses) are cleaned up after a suite is completed. This is helpful in finding out static memory leaks (a class references something huge but is no longer used).
    See Also:
    ClassRule, accept(Field)
    • Field Detail

      • DEFAULT_LEAK_THRESHOLD

        public static final long DEFAULT_LEAK_THRESHOLD
        See Also:
        Constant Field Values
      • leakThreshold

        private final long leakThreshold
      • countSuperclasses

        private final boolean countSuperclasses
    • Constructor Detail

      • StaticFieldsInvariantRule

        public StaticFieldsInvariantRule()
        By default use DEFAULT_LEAK_THRESHOLD as the threshold and count in superclasses.
      • StaticFieldsInvariantRule

        public StaticFieldsInvariantRule​(long leakThresholdBytes,
                                         boolean countSuperclasses)
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement s,
                                                       org.junit.runner.Description d)
        Specified by:
        apply in interface org.junit.rules.TestRule
      • accept

        protected boolean accept​(java.lang.reflect.Field field)
        Returns:
        Return false to exclude a given field from being counted. By default final fields are rejected.