Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find SecurityMockMvcConfigurers

I'am trying to write a spring security test as described here http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#test-mockmvc-setup. I need to import SecurityMockMvcConfigurers as a static import, but my IDE does not find the class.

My pom.xml looks as follows:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.M2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Spring Boot 1.4.0.M2 imports Spring Security 4.0.4.RELEASE. I' can't find the class in this release. Which additional dependency do I need? Or what else I haven't considered?

like image 296
René Winkler Avatar asked Apr 24 '16 15:04

René Winkler


2 Answers

The missing dependency was:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <version>4.0.4.RELEASE</version>
</dependency>
like image 156
René Winkler Avatar answered Oct 23 '22 17:10

René Winkler


The required dependency for the org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers class is the org.springframework.security:spring-security-test dependency.

The question is tagged Spring Boot and Spring Boot provides the dependency as a managed dependency in the spring-boot-starter-parent pom.
Your pom inherits very probably from spring-boot-starter-parent.
So above all don't specify a version without a very good reason and use simply the version inherited from the parent.
That is enough :

<dependencies> 
     ...
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>         
    </dependency>
     ...
<dependencies> 

For example with Spring Boot 2.0.0.M5, the managed version is

  <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <version>5.0.0.M5</version>
  </dependency>

The M5 is not just chance.
These are designed to work "better" together.

Note that org.springframework.boot:spring-boot-starter-test dependency doesn't pull the spring-security-test dependency.

In case you don't use Spring Boot, you should endeavor to specify a spring-security-test version conform to the Core Spring version used by the application.

like image 29
davidxxx Avatar answered Oct 23 '22 18:10

davidxxx