最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Controller tested on Spring Boot Application cannot be created when test is run - Stack Overflow

programmeradmin3浏览0评论

I'm trying to add an integration test to a Spring Boot Rest API application. The application consists of two controllers, and two services. I cannot mention the real names, but a simplification would be something like

  1. package companyname.controller
    • company
    • user
  2. package companyname.service
    • company
    • user
  3. some auxiliary packages under companyname like auth, dto, exception, model, etc.

The application has no DB but depends on certain services to bring data.

As I mentioned, I need to add testing, specifically integration testing. This has been very difficult because I don't have too much experience in Spring. So, trying to follow the following tutorial I create the following test.

package companyname;

import companyname.controller.CompanyController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.Assertions.assertThat;

    @SpringBootTest
    @TestPropertySource(
            locations = "classpath:application-integrationtest.properties")
    @ComponentScan(basePackages = {
            "companyname.controller"
    })
    class CompanyControllerTest {
    
        @Autowired
        private CompanyController controller;
    
        @Test
        void contextLoads() {
            assertThat(controller).isNotNull();
        }
    
    }

The problem is that I received this horrible error (the error is longer but I cut the repetitive parts)

org.springframework.beans.factory.UnsatisfiedDependencyException: 

Error creating bean with name 'companyController' defined in file

path/to/project/server/build/classes/java/main/com/companyname/controller/CompanyController.class]: 
Unsatisfied dependency expressed through constructor parameter 0: 
Error creating bean with name 'companyService' defined in file [/path/to/project/server/build/classes/java/main/com/companyname/service/CompanyService.class]: 

Can anyone point me to what I'm doing wrong?

Thanks in advance

发布评论

评论列表(0)

  1. 暂无评论