关于spring:springbootJunit5测试失败的小问题

21次阅读

共计 1036 个字符,预计需要花费 3 分钟才能阅读完成。

当我应用 springboot+junit5 测试 mybatisplus 拜访数据库时,报异样了,异样如下:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    ..........
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:145)
        ... 29 more

说实话,这么给我报异样,我挺心痛的:@SpringBootTest我曾经有了:

import com.niewj.cloud.domain.User;
import com.niewj.cloud.mapper.UserMapper;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class SampleTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

起因也很简略:test 测试用例的 包构造!

测试用例的包构造要在 springboot 的扫描范畴内才可,比方:
App.java(main 办法所在的类)的包名为:com.niewj.cloud,那测试用例的测试类,也放在这样的目录下就能够了!

正文完
 0