[JUnit] Assumption and assertAll

Assumption:

Run the testing code only if when the condition is match, otherwise, test will be ignored

    @Test
    void runTestIf() {
        System.out.print("Current hour: " + gc.get(Calendar.HOUR_OF_DAY));
        // if not match the asumption, test will be ignored
        Assumptions.assumeTrue(gc.get(Calendar.HOUR_OF_DAY) < 20);
        // following code will be executed only when Assumption match
        firstEmployee.adjustSalary(5000);
        assertEquals(85000, firstEmployee.getSalary());
    }

    @Test
    void runOnlyTestDefined() {
        System.out.print("Current hour: " + gc.get(Calendar.HOUR_OF_DAY));
        Assumptions.assumingThat(gc.get(Calendar.HOUR_OF_DAY) < 20, () -> {
            // only this part of code will be run if assumption is true
            assertEquals(85000, firstEmployee.getSalary());
        });
        System.out.print("After the invocation of assumingTaht will still be excuated no matter what");
    }

 

assertAll:

Check all condiiton in test:

    @Test
    void checkEmployee() {
        firstEmployee.adjustSalary(4000.0);

        assertAll(
                () -> assertEquals(84000, firstEmployee.getSalary()),
                () -> assertNotNull(firstEmployee.getFirstName()),
                () -> assertThrows(NamingException.class, () -> {firstEmployee.validateLastName();})
        );
    }

 

posted @ 2022-08-04 18:28  Zhentiw  阅读(22)  评论(0编辑  收藏  举报