Loading

Spring Junit 测试报错 java.lang.IllegalStateException

写测试代码的时候出现了java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.

代码如下:

package com.example.service;

import com.example.config.SpringConifg;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConifg.class)
public class UserServiceTest {
    @Autowired
    private UserService userService;

    @Test
    public void testSelect()
    {
        System.out.println(userService.select());
    }
}

最后发现是spring-webmvcspring-test 的依赖版本不一致导致的,修改为相同的版本即可解决。

posted @ 2024-06-13 17:37  qianyuzz  阅读(163)  评论(0)    收藏  举报