0

I have a class containing multiple constants and I would like to return all the constants except one in order to make some tests. I have made a simple method but it return the error "attribute value must be constant" when I use it in my annotation.

public final class RolesConstants {

    public static final String APP_SYSTEM = "APP_SYSTEM";
    public static final String APP_CLIENT = "APP_CLIENT";
    public static final String APP_PROFESSIONAL = "APP_PROFESSIONAL";
    public static final String APP_ADMIN = "APP_ADMIN";
    public static final String PRO_ADMIN = "PRO_ADMIN";
    public static final String PRO_ADD_BOOKING = "PRO_ADD_BOOKING";
    public static final String PRO_EDIT_BOOKING = "PRO_EDIT_BOOKING";
    ...

    public static String[] allRolesButAPP_SYSTEM() {
        return new String[]{ APP_CLIENT, APP_PROFESSIONAL, ... };
    }
}


import org.springframework.security.test.context.support.WithMockUser;

public class ProfessionalResourceIT {

    @WithMockUser(roles = {RolesConstants.allRolesButAPP_SYSTEM()})
    public void cannotGetIfNotSystem(){
        assertThat(...);
    }
}

attribute value must be constant

Antoine Grenard
  • 1,712
  • 3
  • 21
  • 41

1 Answers1

0

Could you try something like this

import org.springframework.security.test.context.support.WithMockUser;

public class ProfessionalResourceIT {

    @WithMockUser(roles = {RolesConstants.APP_SYSTEM, RolesConstants.APP_CLIENT, so on})
    public void cannotGetIfNotSystem(){
        assertThat(...);
    }
}
MOnkey
  • 751
  • 6
  • 13
  • @Antoine, have you tried my solution, is it working for you? – MOnkey Feb 22 '20 at 13:22
  • @Antoine,ohh not sure then, is there a way to achieve the same by passing the whole in one go.. – MOnkey Feb 23 '20 at 11:27
  • @Antoine, Unfortunately, you can't do this with arrays, that is y edited my solution also, seems like you need to pass all the value one by one as shown above in my answer, we can have more info here https://stackoverflow.com/a/2067863/7414166 – MOnkey Feb 23 '20 at 12:25
  • Actually that is exactly what I don't want to do (pass them all one by one) as I have more than 10 of "roles" – Antoine Grenard Feb 23 '20 at 12:32
  • @Antoine, yeah can understand but it seems I don't have any other solution right now for this problem.. – MOnkey Feb 23 '20 at 12:34