Reputation: 3386
I am developing a Spring Boot application using STS with the Gradle plugin. I have a different configuration for tests, to prevent our Selenium tests from having to login.
So in src/test/java/etc
I have something like this:
@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.
If I run the application through the Gradle plugin (bootRun), everything works fine.
However, if I run or debug it through Eclipse directly (e.g. 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.
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?
Upvotes: 10
Views: 5214
Reputation: 417
At least Eclipse IDE Version: 2022-12 (4.26.0) has the feature that allows to exclude test code from Debug/Run configuration classpath:
Go to Run/Debug configuration you use to run your app, choose Dependencies tab, click the checkbox 'Exclude test code'.
Upvotes: 1
Reputation: 620
You can append @TestComponent
to you test configuration class. These bean configurations will be skipped during component scan of your application. Depending on the component scan configuration, you need to define an @ComponentScan exclude filter:
excludeFilters = @ComponentScan.Filter(value = TestComponent.class, type = FilterType.ANNOTATION))
Upvotes: 0
Reputation: 9526
When you run the test from eclipse, the classpath is prepared by eclipse (and not by maven or gradle).
Eclipse only uses one classpath per project and does not know anything about dependency scopes (like 'compile' or 'test'). 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.
Upvotes: 2