0

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

anggor
  • 79
  • 1
  • 10
  • Does this answer your question? [Conditionally skip a Junit 5 test](https://stackoverflow.com/questions/60689337/conditionally-skip-a-junit-5-test) – tgdavies Jan 15 '23 at 02:57
  • The result is the same. ```Assumptions.assumeTrue()/assumeFalse()``` is the simplified version of my code and output the exception ```org.opentest4j.TestAbortedException: Assumption failed: assumption is not true``` – anggor Jan 15 '23 at 03:02
  • What is the problem you are encountering with your solution or `Assumptions.assumeTrue()`? – knittl Jan 15 '23 at 08:48
  • My solution and ```Assumptions.assumeTrue()``` will throw exception which will resulting in ```Test failed```. I only want to skip the test. – anggor Jan 15 '23 at 09:03
  • @anggor `Assumptions.assumeTrue()`'s exception will be handled specially by the junit framework and mark the test as skipped, not failed. – knittl Jan 28 '23 at 10:58
  • @knittl I beg to differ. Please to updated question. I uploaded the result when I'm using ```Assumptions.assumeTrue()``` – anggor Feb 02 '23 at 09:02
  • @anggor [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – knittl Feb 02 '23 at 12:23
  • @anggorif you look closely, you see that you are not using `Assumptions` from JUnit 5 (`org.junit.jupiter.api.Assumptions`), but from `org.opentest4j`. Fix your imports, use the correct classes. (Sorry for only noticing now, I should have noticed that from your second comment already: it contains the wrong package name) – knittl Feb 02 '23 at 12:25

0 Answers0