为什么我不能将@WithMockUser导入我的测试

时间:2017-10-14 15:00:37

标签: spring-boot

我正在尝试使用@With Mock User进行身份验证测试,但是它拒绝在spring boot中导入我的测试类。这是类配置

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

@WithMockUser无法导入,其红色显示弹簧启动无法识别它,我使用了此依赖关系和属性

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


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

继续说无法解析符号@WithMockUser

3 个答案:

答案 0 :(得分:6)

尝试新的依赖关系。我刚刚用这个解决了这个问题:

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

答案 1 :(得分:1)

为库添加import语句:

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

并在你的pom中声明一个依赖:

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

答案 2 :(得分:0)

尝试强制gradle刷新依赖关系:

gradle --refresh-dependencies