Code Snippet

Just another Code Snippet site

[JUnit] Parametrized Test

Need Junit 4.11


@RunWith(Parameterized.class)
public class ParametrizedTest {

    /** The Logger. */
    private static final Logger LOGGER = LoggerFactory.getLogger(ParametrizedTest.class);

    /** XML Input Folder. */
    private static File inputFolder = new File("target/classes/input");

    /** XML Target Folder. */
    private static File targetFolder = new File("target/test-classes/output");

    private final File file;
    private final String param;

    /**
     * Default constructor.
     */
    public ParametrizedTest(File file, String param) {
        this.file = file;
        this.param = param;
    }

    /**
     * Prepare env. for test run.
     * 
     * @throws IOException
     */
    public static void initClass() throws IOException {
        LOGGER.info("initClass()");

        targetFolder.delete();
        targetFolder.mkdirs();
    }

    @Parameters(name = "{1}")
    public static Collection<Object[]> data() throws IOException {
        initClass();

        return getAllFiles();
    }

    /**
     * Test to run
     */
    @Test
    public void test_1() {
        LOGGER.warn("------------------------------------------------------------------------");
        LOGGER.warn("Processing : " + param);

        Assert.assertTrue(true);
    }

    /**
     * List files to process.
     * 
     * @return List files to process.
     */
    public static Collection<Object[]> getAllFiles() {
        LOGGER.info("Initialize collection");
        List<Object[]> result = new ArrayList<Object[]>();

        for (File template : targetFolder.listFiles()) {
            result.add(new Object[] { template, template.getName() });
        }

        return result;
    }
}

, ,


One thought on “[JUnit] Parametrized Test

  • Olivier says:
        public static Collection<Object[]> getAllFiles(final String folder) throws IOException {
            LOGGER.info("Initialize collection");
            List<Object[]> result = new ArrayList<Object[]>();
    
            readFolderContent(folder, result);
    
            return result;
        }
    
    
        private static void readFolderContent(File inputFolder, List<Object[]> result) {
            LOGGER.info("Parsing folder <{}> ", inputFolder.getName());
            for (File template : inputFolder.listFiles()) {
    
                if (template.isFile()) {
                    if (template.getName().endsWith("sql")) {
                        LOGGER.debug("File <{}> is added to the list of files to process", template.getName());
                        result.add(new Object[] { template, template.getName() });
    
                    } else {
                        LOGGER.debug("File <{}> is ignored.", template.getName());
                    }
                } else {
                    LOGGER.debug("File <{}> is a directory.", template.getName());
                    readFolderContent(template, result);
                }
            }
        }
    

Leave a Reply

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