Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I had a Springframework-enabled JUnit test that I needed to run multiple times. I thought there used to be a JUnit annotation, but I couldn’t find it. I found and used the Spring @Repeat annotation.

package com.this.and.that;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

import java.util.concurrent.LinkedBlockingQueue;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
@ContextConfiguration
public class TestCardSet {

    @Repeat(10)
    @Test
    public void testGetShuffledCardQueue() throws Exception {
        LinkedBlockingQueue<Card> cards = CardSet.getShuffledCardQueue();
        assertNotNull(cards);
        assertEquals(52, cards.size());
    }
}

I used to set breakpoints to verify that the test was being ran multiple times. The Spring @Repeat annotation removed the need for this by adding logging.

2014-12-02 09:41:05,499 INFO  o.s.beans.factory.xml.XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [com/iteam/poker/server/model/game/cards/TestCardSet-context.xml]
2014-12-02 09:41:05,815 INFO  o.s.context.support.GenericApplicationContext:510 - Refreshing org.springframework.context.support.GenericApplicationContext@4c6cea8a: startup date [Tue Dec 02 09:41:05 EST 2014]; root of context hierarchy
2014-12-02 09:41:05,866 INFO  o.s.b.factory.support.DefaultListableBeanFactory:598 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6f7503eb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
2014-12-02 09:41:05,886 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 1 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,898 INFO  c.i.poker.server.shuffle.SecureShuffleAlgorithm:26 - Cards shuffle algorithm seed has been initialized.
2014-12-02 09:41:05,899 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 2 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,900 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 3 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,901 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 4 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,902 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 5 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,903 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 6 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,903 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 7 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,904 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 8 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,905 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 9 of test TestCardSet#testGetShuffledCardQueue()
2014-12-02 09:41:05,906 INFO  o.s.test.context.junit4.statements.SpringRepeat:69 - Repetition 10 of test TestCardSet#testGetShuffledCardQueue()

Process finished with exit code 0

December 2nd, 2014

Posted In: JUnit, Spring, Test Driven Development, Unit Tests

Tags: , , , , ,

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com