quarkusjunit5parameterizedquarkus-testing

Parameterized QuarkusTest with TestSecurity


I was wondering if there is a way to parameterize the QuarkustTests and TestSecurity can be parameterized? Currently I have something like this

@QuarkusTest
class MyTest {

    @Inject
    Testservice testee;

    @Test
    @TestSecurity(user = "a", roles = "a")
    void testWithRole_a_throwsException() {
        assertThrows(ForbiddenException.class, () -> testee.myMethod());
    }

    @Test
    @TestSecurity(user = "b", roles = "b")
    void testWithRole_b_throwsException() {
        assertThrows(ForbiddenException.class, () -> testee.myMethod());
    }

}

I would be better if I could do something like this:

@QuarkusTest
class MyTest {


    @Inject
    Testservice testee;

    static Stream<TestUser> testUserDataProvider() {
        return Stream.of(
                new TestUser("a", "a"),
                new TestUser("b", "b")
        );
    }

    @ParameterizedTest
    @MethodSource("testUserDataProvider")
    void testWithRole_throwsException(TestUser testUser) {
        TestSecurity annotation = new TestSecurity(testUser.username, testUser.role);
        assertThrows(ForbiddenException.class, () -> testee.myMethod());
    }

    private record TestUser(String username, String role) {}

}

Unfortunately, I have not found anything like this or anything similar anywhere.

Many thanks and regards


Solution

  • No that's not possible, but you could do this:

    @ParameterizedTest
    @CsvSource(emptyValue = " ", textBlock = """
                admin, admin, 200
                wrongUser, wrongPassword, 401
                correctUserWithWrongRole, 12345, 403
                ,,401
              """)
    void checkUser(String username, String password, int expectedStatus) {
       given()
            .header(CONTENT_TYPE, APPLICATION_JSON)
            .auth().basic(username, password)
       .when()
           .get("/api/fruits")
       .then()
           .statusCode(expectedStatus);
    }
    
    @ParameterizedTest
    @CsvSource(textBlock = """
                GET, /api/fruits, 200
                DELETE, /api/fruits, 403
                GET, /api/forbidden-fruits, 403
               //etc.
              """)
    @TestSecurity(user = "correctUser", roles = {"notAdmin", "user"})
    void checkAuthorization(String method, String path, int expectedStatus) {
        given()
             .header(CONTENT_TYPE, APPLICATION_JSON)
        when()
             .request(method, path)
        then()
             .statusCode(expectedStatus);
    }