How to stop/skip Parameterize test without throwing exception?

70 Views Asked by At

I want to try to stop the test when the param == 3. I found a way to achieve this by using Disable(). However, this approach uses throw. Is there any way I can do this without throwing exceptions?

The idea is,@RunWith(Parameterized.class)annotation will make sure all the tests run according to how many values I have, in this case, 3. getPdInit_Level1_SUCCESSdoes not have a level 3 user but another test, example getPdInit_Level3_SUCCESS contain implementation until level 3. So I only want to run getPdInit_Level1_SUCCESS 2 times and getPdInit_Level3_SUCCESS 3 times.

private static class Disable {
        
        private final Object[] arguments;
        
        private Disable(Object[] arguments) {
            this.arguments = arguments;
        }
        
        public static Disable when(Object... arguments) {
            return new Disable(arguments);
        }
        
        public Disable is(Object... values) {
            if (Arrays.equals(arguments, values))
                throw new TestAbortedException("Test aborted for arguments " + Arrays.toString(values));
            return this;
        }
        
    }

        @Parameterized.Parameter(value = 0)
        public int fInput;
        
        @Parameterized.Parameter(value = 1)
        public int fOutput;
        
        @Parameterized.Parameters
        public static Collection userLevel() {
            return Arrays.asList(new Object[][]{
                    {1, 3},
                    {2, 2},
                    {3, 3},
            });
        }

        @Test
        public void getPdInit_Level1_SUCCESS() {
        
            Disable.when(fInput, fOutput)
                    .is(3, 3);
            
            //Real test here
}

When using Assumptions.assumeTrue(), this is what I get :Test Result

0

There are 0 best solutions below