jeudi 28 septembre 2017

@Configuration not getting loaded when in test package

Introduction

I have a repository PermissionRep that collects Permission beans (defined by other services) and uses it as resource.

A example service would define it's Permission beans to allow access this way:

@Configuration
public class SomePermissions extends Permissions {

    private static final String BASE = "some";
    public static final String SOME_ACCESS = join(BASE, "access");

    @Bean()
    Permission getAccessPermission() {
    return new Permission(SOME_ACCESS);
    }
}

Problem

Now would like to test my repository PermissionRep. As there is no other service available during the test, there will be no Permission beans. Therefor I would like to use a TestPermissions configuration class like:

@Configuration
public class TestPermissions extends Permissions {

    private static final String BASE = "test";
    public static final String TEST_READ = join(BASE, READ);
    public static final String TEST_WRITE = join(BASE, WRITE);

    @Bean()
    Permission getReadPermission() {
    return new Permission(TEST_READ);
    }

    @Bean()
    Permission getWritePermission() {
    return new Permission(TEST_WRITE);
    }
}

I did create this in my test package, but Spring won't load the configuration class. If I point to the configuration class @ContextConfiguration(classes = TestPermissions.class) the defined beans are getting initialized. But therefor the PermissionRep bean which I want to test is not getting created.

How can I add a @Configuration class for testing purposes to my test environment, without messing with the default configurations?

Aucun commentaire:

Enregistrer un commentaire