我是第一次使用spring创建应用程序的后端。我写了我的第一个持久层测试。这只是为了在我的持久层中进行读写。但是,出现以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ ca.mcgill.ecse321.petshelter.dao.TestPetShelterPersistence”的bean时出错:通过字段“ clientRepository”表示不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'ca.mcgill.ecse321.petshelter.dao.ClientRepository'的合格Bean:应该至少有1个符合自动装配候选条件的bean。依赖性注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

即使经过大量研究,我也无法找到任何解决方案。

这是我的客户资料库:

package ca.mcgill.ecse321.petshelter.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import ca.mcgill.ecse321.projectgroup16.Client;


public interface ClientRepository extends CrudRepository<Client, String> {

    Client findClientByEmail(String email);

}


这是我的测试课:

package ca.mcgill.ecse321.petshelter.dao;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.sql.Date;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import ca.mcgill.ecse321.projectgroup16.Client;

@ContextConfiguration(classes = {Client.class})
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class TestPetShelterPersistence {

    @Autowired
    private ClientRepository clientRepository;

    @Test
    public void testCreateClientAndFind() {

        Client u = new Client();
        u.setName("joseph");
        u.setEmail("[email protected]");

        clientRepository.save(u);

        Client b = clientRepository.findClientByEmail("[email protected]");
        assertNotNull(b);
        assertEquals("[email protected]",b.getEmail());
    }
}


非常抱歉,如果有很多代码,谢谢您的帮助!

最佳答案

用@Repository批注注释ClientRepository类。否则,ClientRepository无法自动连接。

@Repository
    public interface ClientRepository extends CrudRepository<Client, String> {

        Client findClientByEmail(String email);

    }

09-26 09:11