Teaser Image

qnoid

Markos Charatzas - London, UK




public class MySelfTest
    {
        @Test
        public void goOutWithYourMates() throws Exception
        {
            MySelf mySelf = new MySelf();
            
            /**
             * Not DRY
             */
            mySelf.getOver( new ExGirlfriend() );
            
            Assert.assertTrue( mySelf.goOutWithYourMates() );        
        }
        
        @Test
        public void playStarcraft() throws Exception
        {
            MySelf mySelf = new MySelf();

            /**
             * Not DRY
             */
            mySelf.getOver( new ExGirlfriend() );
            
            Assert.assertTrue( mySelf.playStarcraft() );        
        }
    }

    /**
     * DRY yourself
     */

    /**
     * Create implementations of this interface with your conditions
     */
    public interface Condition
    {
        /**
         * Put the condition logic here 
         */
        public void apply();
    }


    /**
     * Introduce Preconditions
     */
    class Preconditions
    {
        static Condition all(final Condition... conditions)
        {
        return new Condition() {            
                @Override
                public void apply()
                {
                    for (Condition condition : conditions)
                    {
                        condition.apply();
                    }                
                }
            };
        }
        /**
         * @return
         */
        static Condition buggageShouldBeLeftBehind(final MySelf mySelf, final Girlfriend girlfriend)
        {
        return new Condition() {        
                @Override
                public void apply(){
                    mySelf.getOver( girlfriend );
                }
            };
        }
    }

    /**
     * Leave buggage behind
     *
     */
    public class MySelfTest
    {
        @Test
        public void goOutWithYourMates() throws Exception
        {
            MySelf mySelf = new MySelf();
            
            Preconditions.buggageShouldBeLeftBehind(mySelf, new ExGirlfriend()).apply();
            
            Assert.assertTrue( mySelf.goOutWithYourMates() );        
        }
        
        @Test
        public void playStarcraft() throws Exception
        {
            MySelf mySelf = new MySelf();

            Preconditions.buggageShouldBeLeftBehind(mySelf, new ExGirlfriend()).apply();
            
            Assert.assertTrue( mySelf.playStarcraft() );        
        }
    }

Use a class to define your Preconditions.

You don't pollute your test class with methods not related explicitly to your tests and avoid boilerplate code.

Preconditions class

  • encapsulates the Condition(s)

while making your code

  • readable
  • reusable

Can apply to any conditions you would like to encapsulate really.

Disclaimer: It's a trivial example to make you think of an alternative way to apply pre/post conditions to your tests.