Spring Boot 本机查询的Junit基本测试无法正常工作

nhhxz33t  于 2023-02-16  发布在  Spring
关注(0)|答案(1)|浏览(150)

我尝试在SpringBoot应用程序中使用JUnit测试一些查询,我得到这个错误:

NoSuchBeanDefinitionException: No qualifying bean of type DocumentRepositoryTest
@Repository
public interface DocumentRepositoryTest extends DocumentRepository {

    @Query("SELECT * " +
            "FROM document d" +
            "WHERE d.id = :id")
    Object runQuery(@NonNull Integer id);
}

@SpringBootTest
public class DocumentServiceTest {

    @Autowired
    protected DocumentRepositoryTest documentRepositoryTest;

    @Test
    void testQuery() {
        Object o = documentRepositoryTest.runQuery(2);
    }
}

我不明白为什么我有这个问题?

pb3skfrl

pb3skfrl1#

确保DocumentRepository正在通过Spring组件扫描进行扫描。这可以通过将@ComponentScan注解添加到@SpringBootTest类来完成。示例:

@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan(basePackages = "com.example.repository")
public class DocumentServiceTest {

    @Autowired
    protected DocumentRepositoryTest documentRepositoryTest;

    @Test
    void testQuery() {
        Object o = documentRepositoryTest.runQuery(2);
    }
}

相关问题