Constructor Initialization

Definition:

  • Ideally, the test suite should not have a constructor. Initialization of fields should be in the setUp() method. Developers who are unaware of the purpose of setUp() method would give rise to this smell by defining a constructor for the test suite.

Code Example:

public class TagEncodingTest extends BrambleTestCase {
        private final CryptoComponent crypto;
        private final SecretKey tagKey;
        private final long streamNumber = 1234567890;

        public TagEncodingTest() {
                crypto = new CryptoComponentImpl(new TestSecureRandomProvider());
                tagKey = TestUtils.getSecretKey();
        }

        @Test
        public void testKeyAffectsTag() throws Exception {
                Set set = new HashSet<>();
                for (int i = 0; i < 100; i++) {
                        byte[] tag = new byte[TAG_LENGTH];
                        SecretKey tagKey = TestUtils.getSecretKey();
                        crypto.encodeTag(tag, tagKey, PROTOCOL_VERSION, streamNumber);
                        assertTrue(set.add(new Bytes(tag)));
                }
        }
...
}

References:

Quality attributes

  • - Code Example

  • - Cause and Effect

  • - Frequency

  • - Refactoring