简体   繁体   English

Spring - Autowired 是 null - 测试 - UnsatisfiedDependencyException: Error creating bean with name

[英]Spring - Autowired is null - Test - UnsatisfiedDependencyException: Error creating bean with name

I'm trying to test my application, I've been trying to solve it for 3 days and I looked for stackoverflow and I still couldn't solve it.我正在尝试测试我的应用程序,我已经尝试解决它 3 天了,我寻找了 stackoverflow,但我仍然无法解决它。 My problem is that Autowired is always null, and even though I import everything suggested as我的问题是 Autowired 始终是 null,即使我将所有建议的内容导入为

@RunWith( SpringRunner.class )
@SpringBootTest
public class ESGControllerTest {
    @Autowired
    private ESGController esgController ;
    @Test
    public void deveRetornarSucesso_QuandoBuscarLatLong(){
        System.out.println(this.esgController);
    }

}

or或者

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration
public class ESGControllerTest {
    @Autowired
    private ESGController esgController ;
    @Test
    public void deveRetornarSucesso_QuandoBuscarLatLong(){
        System.out.println(this.esgController);
    }

}

is always null and gives this error始终为 null 并给出此错误

在此处输入图像描述

EDIT: ESGController编辑:ESG控制器

package br.com.kmm.esgeniusapi.controller;

import br.com.kmm.esgeniusapi.dto.CargaDTO;
import br.com.kmm.esgeniusapi.dto.CargaFilter;
import br.com.kmm.esgeniusapi.entity.AreaEmbargada;
import br.com.kmm.esgeniusapi.entity.Carga;
import br.com.kmm.esgeniusapi.entity.ConfiguracaoCarga;
import br.com.kmm.esgeniusapi.exception.CargaException;
import br.com.kmm.esgeniusapi.inteface.HereReverseGeocode;
import br.com.kmm.esgeniusapi.inteface.HereSearch;
import br.com.kmm.esgeniusapi.service.CargaService;
import br.com.kmm.esgeniusapi.service.ConfiguracaoCargaService;
import br.com.kmm.esgeniusapi.service.IbamaService;
import br.com.kmm.esgeniusapi.service.ReverseGeocodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/api/")
@RolesAllowed("VIEW")
@Component
public class ESGController {

    @Autowired
    ReverseGeocodeService reverseGeocodeService;

    @Autowired
    IbamaService ibamaService;

    @Autowired
    CargaService cargaService;

    @Autowired
    ConfiguracaoCargaService configuracaoCargaService;

    @GetMapping(path = "reverse-geocode")
    public HereReverseGeocode getRoute(@RequestParam final String location) {
        Double lat = Double.parseDouble(location.split(",")[0]);
        Double lon = Double.parseDouble(location.split(",")[1]);
        return this.reverseGeocodeService.getReverseGeocoding(lat, lon);
    }

    @GetMapping(path = "search")
    public List<HereSearch> search(@RequestParam(name = "q") final String query) {
        return this.reverseGeocodeService.search(query);
    }
     ....{
    //MORE FUNCTIONS
     }
}

I edited and put the ESGController as code for more information.我编辑了 ESGController 并将其作为代码放置以获取更多信息。

Is ESGController decorated with @Controller or equivalent so that a bean of that class actually exist in the context? ESGController 是否装饰有 @Controller 或等效物,以便 class 的 bean 实际上存在于上下文中?

Is the test class in the same package hierarchy as the rest of the application?测试 class 是否与应用程序的 rest 在相同的 package 层次结构中?

@SpringBootTest by default starts searching in the current package of the test class and then searches upwards through the package structure, looking for a class annotated with @SpringBootConfiguration from which it then reads the configuration to create an application context. @SpringBootTest 默认开始在测试class 的当前package 中搜索,然后向上搜索package 结构,寻找带有@SpringBootConfiguration 注释的class,然后从中读取配置以创建应用程序上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnsatisfiedDependencyException - 创建具有名称的 bean 时出错 - UnsatisfiedDependencyException - Error creating bean with name UnsatisfiedDependencyException:使用名称创建 bean 时出错 - UnsatisfiedDependencyException: Error creating bean with name Spring errorCaused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“test”的 bean 时出错 - Spring errorCaused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test' UnsatisfiedDependencyException:在 Junit 测试中创建名为“repository.BookRepositoryTest”的 bean 时出错 - UnsatisfiedDependencyException: Error creating bean with name 'repository.BookRepositoryTest' in Junit Test Spring Boot UnsatisfiedDependencyException 创建名称为不可解析循环引用的 bean 时出错 - Spring Boot UnsatisfiedDependencyException Error creating bean with name unresolvable circular reference 错误:UnsatisfiedDependencyException:创建名为“entityManagerFactory”的 bean 时出错 - Error: UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' UnsatisfiedDependencyException:创建名为“entityManagerFactory”的 bean 时出错 - UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' UnsatisfiedDependencyException:创建名称为“ trafficMapper”的bean时出错 - UnsatisfiedDependencyException: Error creating bean with name 'trafficMapper' UnsatisfiedDependencyException:创建名称为“requestMappingHandlerAdapter”的 bean 时出错 - UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' Jenkins 构建 UnsatisfiedDependencyException:创建带有名称的 bean 时出错 - Jenkin build UnsatisfiedDependencyException: Error creating bean with name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM