关于Junit中autowired dependencies failed的问题

版权声明: https://blog.csdn.net/qxconverse/article/details/80530421

关于这个错误,找了很久。原来是自己对于junit测试相关的东西不懂。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationTests': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private me.xqcv.service.UserService ApplicationTests.userSerivce; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [me.xqcv.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]

ApplicationTests文件中,@SpringApplicationConfiguration(ApplicationC4.class)的意思我一开始因为直接复制别人的代码,而直接拿过来@SpringApplicationConfiguration(Application.class),当时以为Application是某个库里面的类,所以就引用了。后来才发现,这个应该填SpringBoot的入口类名,我自己的入口类是ApplicationC4

好了,这是一点。关于另外一点就是test/java底下的文件路径问题。我因为懒,所以没有写包名,直接新建的文件,如果没有这行代码import me.xqcv.ApplicationC4;,会提示你加进去,不然找不到入口类。后面继续做实验,发现其实包名结构如果和main/java底下的一致,那么就不需要导入import me.xqcv.ApplicationC4;。如果包名结构不一样,那么就需要强制导入。下面的代码ApplicationTests类是我之前的,即test/java底下没有任何路径。

import me.xqcv.ApplicationC4;
import me.xqcv.service.UserService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(ApplicationC4.class)
public class ApplicationTests {

    @Autowired
    private UserService userSerivce;

    @Before
    public void setUp() {
        // 准备,清空user表
        userSerivce.deleteAllUsers();
    }

    @Test
    public void test() throws Exception {
        // 插入5个用户
        userSerivce.create("a", 1);
        userSerivce.create("b", 2);
        userSerivce.create("c", 3);
        userSerivce.create("d", 4);
        userSerivce.create("e", 5);

        // 查数据库,应该有5个用户
        Assert.assertEquals(5, userSerivce.getAllUsers().intValue());

        // 删除两个用户
        userSerivce.deleteByName("a");
        userSerivce.deleteByName("e");

        // 查数据库,应该有5个用户
        Assert.assertEquals(3, userSerivce.getAllUsers().intValue());

    }
}

下面是我改了之后项目的结构图,这个时候由于我test/java底下的路径和main/java底下的一样,都是me.xqcv,所以就不需要import me.xqcv.ApplicationC4;,而直接package me.xqcv;就ok了:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qxconverse/article/details/80530421