valik
valik

Reputation: 2094

Why cant i import @WithMockUser to my test

I am trying to do an authentication test with @With Mock User but it is refusing to import to my test class in spring boot . This is the class configuration

@WebAppConfiguration
@AutoConfigureMockMvc
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AdminPortalApplication.class)
public class BookControllerTest {

@WithMockUser not able to import ,its red in color showing that spring boot does not recognize it, I used this dependency and property

<spring-security.version>4.0.2.RELEASE</spring-security.version>


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

Keeps saying cannot resolve symbol @WithMockUser

Upvotes: 33

Views: 27378

Answers (3)

Greg
Greg

Reputation: 2617

Try forcing gradle to refresh your dependencies:

gradle --refresh-dependencies

Upvotes: 0

Anton Nakonechnyi
Anton Nakonechnyi

Reputation: 796

Try fresh dependency. I just solved such issue with this one:

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

Upvotes: 61

Boston Hack
Boston Hack

Reputation: 35

Add an import statement for the library:

import org.springframework.security.test.context.support.WithMockUser;

And declare a dependency in your pom:

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

Upvotes: 1

Related Questions