[英]Running Spring Boot application through Eclipse picks up test classes
I am developing a Spring Boot application using STS with the Gradle plugin. 我正在使用带有Gradle插件的STS开发Spring Boot应用程序。 I have a different configuration for tests, to prevent our Selenium tests from having to login.
我有不同的测试配置,以防止我们的Selenium测试必须登录。
So in src/test/java/etc
I have something like this: 所以在
src/test/java/etc
我有这样的东西:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public static class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().anyRequest().permitAll();
}
}
Whereas in src/main/java
I have an equivalent class that configures login etc, requiring login for all pages. 而在
src/main/java
我有一个等效的类可配置login等,要求所有页面都登录。
If I run the application through the Gradle plugin (bootRun), everything works fine. 如果我通过Gradle插件(bootRun)运行该应用程序,则一切正常。
However, if I run or debug it through Eclipse directly (eg right clicking on the project, Run As->Spring Boot App or by clicking the run/debug buttons in the Spring or Java view) then the test config is applied, so access is granted to all pages without login. 但是,如果我直接通过Eclipse运行或调试它(例如,右键单击项目,Run As-> Spring Boot App或通过单击Spring或Java视图中的运行/调试按钮),则将应用测试配置,因此访问无需登录即可授予所有页面。
I'm guessing that the test classes are being included in the classpath when I start the application this way. 我猜测以这种方式启动应用程序时,测试类已包含在类路径中。 Is there an easy way to prevent this from happening?
有没有简单的方法可以防止这种情况发生?
When you run the test from eclipse, the classpath is prepared by eclipse (and not by maven or gradle). 从eclipse运行测试时,类路径是通过eclipse(而不是通过maven或gradle)准备的。
Eclipse only uses one classpath per project and does not know anything about dependency scopes (like 'compile' or 'test'). Eclipse每个项目仅使用一个类路径,并且对依赖范围(例如“编译”或“测试”)一无所知。 So the classpath always contains any resources of a referenced project.
因此,类路径始终包含引用项目的任何资源。
You cannot change this behavior of eclipse. 您无法更改这种日食行为。 You need to use naming conventions, profile etc. to avoid accidental use of test resources.
您需要使用命名约定,配置文件等,以避免意外使用测试资源。
You can append @TestComponent
to you test configuration class. 您可以将
@TestComponent
附加到测试配置类。 These bean configurations will be skipped during component scan of your application. 在应用程序的组件扫描期间,将跳过这些bean配置。 Depending on the component scan configuration, you need to define an @ComponentScan exclude filter:
excludeFilters = @ComponentScan.Filter(value = TestComponent.class, type = FilterType.ANNOTATION))
根据组件扫描配置,您需要定义一个@ComponentScan排除过滤器:
excludeFilters = @ComponentScan.Filter(value = TestComponent.class, type = FilterType.ANNOTATION))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.