Unsatisfied dependency during test

自古美人都是妖i 提交于 2021-02-07 07:41:49

问题


I have a spring boot 2.0.0 M2 application who run well.

I use autowired on constructor

@RequestMapping(value = "/rest")
@RestController
public class AddressRestController extends BaseController{

    private final AddressService AddressService;

    @Autowired
    public AddressRestController(final AddressService AddressService) {
        this.AddressService = AddressService;
    }
    ...
}

@Service
public class AddressServiceImpl extends BaseService implements AddressService {

    @Autowired
    public AddressServiceImpl(final AddressRepository AddressRepository) {
        this.AddressRepository = AddressRepository;
    }

    private final AddressRepository AddressRepository;
    ...
}


public interface AddressRepository extends JpaRepository<Address, Integer>, AddressRepositoryCustom {

}

@Repository
public class AddressRepositoryImpl extends SimpleJpaRepository implements AddressRepositoryCustom {
    @PersistenceContext
    private EntityManager em;

    @Autowired
    public AddressRepositoryImpl(EntityManager em) {
        super(Address.class, em);
    }
    ...
}

When i try to run a basic test

@RunWith(SpringJUnit4ClassRunner.class)
public class AddressServiceTest {

    @Autowired
    private AddressService service;

    @MockBean
    private AddressRepository restTemplate; 

    @Test
    public void getAddress(){

        MockitoAnnotations.initMocks(this);

        Pageable page = PageRequest.of(0, 20);

        Page<Address> pageAdr = mock(Page.class);

        given(this.restTemplate.findAll(page)).willReturn(pageAdr);

        Page<AddressDto> pageDto = service.getAddress(page);

    }
}

I get this error

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.sonos.arcor.service.AddressServiceTest': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ''com.sonos.arcor.service.AddressService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I don't understand why i get this error.


回答1:


You need to annotate the test with SpringBootTest so that spring initialize an application context

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class AddressServiceTest {
// the remaining test
}

Also you do not need MockitoAnnotations.initMocks(this);

Spring takes care of the mock handling

When [@MockBean is]used on a field, the instance of the created mock will also be injected. Mock beans are automatically reset after each test method

see Mocking and spying beans



来源:https://stackoverflow.com/questions/45088711/unsatisfied-dependency-during-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!