SpringBoot Junit bean自动接线 [英] SpringBoot Junit bean autowire

查看:81
本文介绍了SpringBoot Junit bean自动接线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将junit测试写入我的服务. 我在我的项目spring-boot 1.5.1中使用.一切正常,但是当我尝试自动装配bean(在AppConfig.class中创建)时,它给了我NullPointerException.我已经尝试了几乎所有东西.

I try to write junit test to my service. I use in my project spring-boot 1.5.1. Everything works fine, but when I try to autowire bean (created in AppConfig.class), it gives me NullPointerException. I've tried almost everything.

这是我的配置类:

@Configuration 
public class AppConfig {

@Bean
public DozerBeanMapper mapper(){
    DozerBeanMapper mapper = new DozerBeanMapper();
    mapper.setCustomFieldMapper(new CustomMapper());
    return mapper;
 }
}

和我的测试班:

@SpringBootTest
public class LottoClientServiceImplTest {
@Mock
SoapServiceBindingStub soapServiceBindingStub;
@Mock
LottoClient lottoClient;
@InjectMocks
LottoClientServiceImpl lottoClientService;
@Autowired
DozerBeanMapper mapper;

@Before
public void setUp() throws Exception {
    initMocks(this);
    when(lottoClient.soapService()).thenReturn(soapServiceBindingStub);
}

@Test
public void getLastResults() throws Exception {

    RespLastWyniki expected = Fake.generateFakeLastWynikiResponse();
    when(lottoClient.soapService().getLastWyniki(anyString())).thenReturn(expected);

    LastResults actual = lottoClientService.getLastResults();

有人可以告诉我怎么了吗?

Can someone tells me what's wrong ?

错误日志:

java.lang.NullPointerException
at pl.lotto.service.LottoClientServiceImpl.getLastResults(LottoClientServiceImpl.java:26)
at pl.lotto.service.LottoClientServiceImplTest.getLastResults(LottoClientServiceImplTest.java:45)

这是我的服务

@Service
public class LottoClientServiceImpl implements LottoClientServiceInterface {
@Autowired
LottoClient lottoClient;
@Autowired
DozerBeanMapper mapper;
@Override
public LastResults getLastResults() {
    try {
        RespLastWyniki wyniki = lottoClient.soapService().getLastWyniki(new Date().toString());
        LastResults result = mapper.map(wyniki, LastResults.class);
        return result;
    } catch (RemoteException e) {
        throw new GettingDataError();
    }
}

推荐答案

当然,由于您正在创建新实例的@InjectMocks,依赖关系将为null,这在Spring的可视范围之外,因此没有任何作用自动连线.

Ofcourse your dependency will be null,due to the @InjectMocks you are creating a new instance, outside the visibility of Spring and as such nothing will be auto wired.

Spring Boot具有广泛的测试支持,也可以用模拟代替bean,请参见

Spring Boot has extensive testing support and also for replacing beans with mocks, see the testing section of the Spring Boot reference guide.

要修复此问题,请使用框架而不是框架.

To fix it work with the framework instead of around it.

  1. @Mock替换为@MockBean
  2. @InjectMocks替换为@Autowired
  3. 删除您的设置方法
  1. Replace @Mock with @MockBean
  2. Replace @InjectMocks with @Autowired
  3. Remove your setup method

显然,您只需要对SOAP存根进行模拟(因此不确定对于LottoClient而言,您需要模拟什么).

Also apparently you only need a mock for the SOAP stub (so not sure what you need to mock for the LottoClient for).

类似的事情应该可以解决问题.

Something like this should do the trick.

@SpringBootTest
public class LottoClientServiceImplTest {

    @MockBean
    SoapServiceBindingStub soapServiceBindingStub;

    @Autowired
    LottoClientServiceImpl lottoClientService;

    @Test
    public void getLastResults() throws Exception {

        RespLastWyniki expected = Fake.generateFakeLastWynikiResponse();
        when(soapServiceBindingStub.getLastWyniki(anyString())).thenReturn(expected);

        LastResults actual = lottoClientService.getLastResults();

这篇关于SpringBoot Junit bean自动接线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆