Spring profiles on integration tests class

人盡茶涼 提交于 2019-12-11 23:23:00

问题


we have selenium tests which are ran by java test class.

On local environment everything is ok, but I want to switch off those tests when run on jenkins.

So I use:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest("server.port=1234")
@Profile("!jenkins")
@ActiveProfiles("integrationtests")
public class LoginAndEditProfileSeleniumTest {
...

What works: running mvn clean test run all tests locally, with integrationtests profile active. I dont want to pass any additional parameter.

What I want to achieve: running mvn clean test -Dspring.profiles.active=jenkins switch off this test.

Can I merge somehow profile passed by parameter, ActiveProfile annotation and take Profile annotation into consideration? :)

//update: Its possible to use class extending ActiveProfilesResolver:

public class ActiveProfileResolver implements ActiveProfilesResolver {
@Override
public String[] resolve(Class<?> testClass) {

    final String profileFromConsole = System.getProperty("spring.profiles.active");
    List<String> activeProfiles = new ArrayList<>();
    activeProfiles.add("integrationtests");
    if("jenkins".contains(profileFromConsole)){
        activeProfiles.add("jenkins");
    }
    return activeProfiles.toArray(new String[activeProfiles.size()]);
}
}

but it seems to not to cooperate with @Profile anyway ( jenkins profile is active but test is still running ) .


回答1:


@Profile has zero affect on test classes. Thus, you should simply remove that annotation.

If you want to enable a test class only if a given system property is present with a specific value, you could use @IfProfileValue.

However, in your scenario, you want to disable a test class if a given system property is present with a specific value (i.e., if spring.profiles.active contains jenkins).

Instead of implementing a custom ActiveProfileResolver, a more elegant solution would be to use a JUnit assumption to cause the entire test class to be ignored if the assumption fails.

This should work nicely for you:

import static org.junit.Assume.*;

// ...

@BeforeClass
public static void disableTestsOnCiServer() {
    String profilesFromConsole = System.getProperty("spring.profiles.active", "");
    assumeFalse(profilesFromConsole.contains("jenkins"));
}

Regards,

Sam (author of the Spring TestContext Framework)



来源:https://stackoverflow.com/questions/32859317/spring-profiles-on-integration-tests-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!