Junit 5 Test Class Parallel and Test Case Sequential Execution

when we have a lot of jutni 5 test cases, its better to segregate them in different classes accoudingly. Runing multiple test cases class with nested test case will run them randomly.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 + appleBag2;
    }

    public int subApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 - appleBag2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppleCalculatorAddTest {
    @Test
    void addAppleTestCase1() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase1");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase2() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase2");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase3() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase3");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase4() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase4");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase5() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase5");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppleCalculatorSubTest {
    @Test
    void subAppleTestCase1() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase1");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase2() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase2");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase3() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase3");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase4() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase4");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase5() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase5");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }
}
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent

 

we needs to run different test case class parallelly but all the test case withing a perticular class must run sequentially. to achive this junit 5 provides another configuraiton property.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 + appleBag2;
    }

    public int subApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 - appleBag2;
    }
}

 

package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppleCalculatorAddTest {
    @Test
    void addAppleTestCase1() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase1");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase2() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase2");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase3() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase3");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase4() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase4");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase5() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase5");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppleCalculatorSubTest {
    @Test
    void subAppleTestCase1() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase1");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase2() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase2");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase3() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase3");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase4() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase4");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase5() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase5");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }
}
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent

 

if we needs to run different test case class sequentially but all the test case withing a perticular class must run parallelly. to achive this junit 5 provides another configuraiton property.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 + appleBag2;
    }

    public int subApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 - appleBag2;
    }
}

 

package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppleCalculatorAddTest {
    @Test
    void addAppleTestCase1() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase1");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase2() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase2");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase3() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase3");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase4() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase4");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase5() throws InterruptedException {
        System.out.println("AppleCalculatorAddTest.addAppleTestCase5");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppleCalculatorSubTest {
    @Test
    void subAppleTestCase1() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase1");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase2() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase2");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase3() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase3");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase4() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase4");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    void subAppleTestCase5() throws InterruptedException {
        System.out.println("AppleCalculatorSubTest.subAppleTestCase5");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }
}
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = same_thread

 

follow us on