简体   繁体   English

通过Eclipse运行Spring Boot应用程序将获取测试类

[英]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.

相关问题 Spring Boot @Value 注解从 application.properties 中获取密钥,但在运行时不使用它 - Spring Boot @Value annotation picks up key from application.properties, but does not use it at run time Spring Boot 应用程序正在从 JUnit 测试启动,但无法通过 URL 访问 - Spring Boot Application is starting up from a JUnit test but unable to access through URL Tomcat 通过 Jar 运行 Spring 引导应用程序时不出现(缺少 ServletWebServerFactory bean) - Tomcat Doesn't Come Up When Running Spring Boot Application Through the Jar (missing ServletWebServerFactory bean) 如何在一次测试中启动2个春季启动应用程序? - How to boot up 2 spring boot application in one test? 如何在 Spring 引导应用程序启动时自动验证 @Configuration 类? - How are @Configuration classes validated automatically on Start Up of Spring Boot application? 使用Spring Boot Test运行测试 - Running test with Spring Boot Test 从 Spring Boot 集成测试运行原始应用程序 - Running the original application from Spring Boot integration test Spring Boot未在Eclipse中与Tomcat一起运行 - Spring boot not running with Tomcat in Eclipse Eclipse选择错误的JDK - Eclipse picks up wrong jdk 如何在 spring 启动应用程序状态之前运行wiremock? - How to get wiremock running before the spring boot application status up?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM