Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Spring Boot application through Eclipse picks up test classes

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?

like image 677
Martin Wilson Avatar asked Nov 14 '15 11:11

Martin Wilson


People also ask

Can we run spring boot application in Eclipse?

Installation. You can install the Spring Tools for Eclipse IDE into an existing Eclipse installation using the Eclipse Marketplace. Just open the marketplace client in Eclipse, search for Spring Tools and install the “Spring Tools (aka Spring IDE and Spring Tool Suite)” entry.

How do you write a test case for spring boot application class?

Then, configure the Application context for the tests. The @Profile(“test”) annotation is used to configure the class when the Test cases are running. Now, you can write a Unit Test case for Order Service under the src/test/resources package. The complete code for build configuration file is given below.


1 Answers

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.

like image 120
Stefan Isele - prefabware.com Avatar answered Oct 15 '22 04:10

Stefan Isele - prefabware.com