gpt4 book ai didi

java - 注入(inject)的 Bean 在抽象类中为 null

转载 作者:行者123 更新时间:2023-12-02 10:07:37 25 4
gpt4 key购买 nike

我有抽象用户服务,我在其中 Autowiring 了两个 bean:Repository 和 AbstractMapper,但是当我对该类运行测试时,全部失败并出现 NullPointerException。例如,当我运行在 dubug 中保存该服务的测试时,它显示两个 bean 都为空。有人遇到这个问题吗?这是我的代码:

抽象服务

package com.example.shop.service;

import com.example.shop.dto.AbstractMapper;
import com.example.shop.entity.Identifable;
import com.example.shop.repository.IRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service`enter code here`
public abstract class AbstractService<E extends Identifable, D> implements IService<E, D> {

private IRepository<E> repository;
private AbstractMapper<E, D> abstractMapper;

@Autowired
public AbstractService(IRepository<E> repository, AbstractMapper<E, D> abstractMapper) {
this.repository = repository;
this.abstractMapper = abstractMapper;
}

@Override
public D get(Long id) {
E e = repository.get(id);
return abstractMapper.entityToDto(e);
}

@Override
public List<D> getAll() {
List<E> all = repository.getAll();
List<D> allDtos = all.stream()
.map(e -> abstractMapper.entityToDto(e))
.collect(Collectors.toList());
return allDtos;
}

@Override
public E save(D d) {
E e = abstractMapper.dtoToEntity(d);
return repository.save(e);
}

@Override
public E update(D d) {
E e = abstractMapper.dtoToEntity(d);
return repository.update(e);
}

@Override
public E delete(D d) {
E e = abstractMapper.dtoToEntity(d);
return repository.delete(e);
}

@Override
public void deleteAll() {
repository.deleteAll();
}
}

用户服务Impl

package com.example.shop.service;

import com.example.shop.dto.UserDto;
import com.example.shop.dto.UserMapper;
import com.example.shop.entity.User;
import com.example.shop.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends AbstractService<User, UserDto> implements UserService {

private UserRepository repository;
private UserMapper userMapper;

@Autowired
public UserServiceImpl(UserRepository repository, UserMapper userMapper) {
super(repository, userMapper);
}
}

抽象映射器

package com.example.shop.dto;

import org.springframework.stereotype.Component;

@Component
public interface AbstractMapper<E, D> {

E dtoToEntity(D d);
D entityToDto(E e);
}

用户映射器:

package com.example.shop.dto;

import com.example.shop.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserMapper implements AbstractMapper<User, UserDto> {

private AccountMapper accountMapper;

@Autowired
public UserMapper(AccountMapper accountMapper) {
this.accountMapper = accountMapper;
}

@Override
public User dtoToEntity(UserDto dto) {
if (dto == null) {
return null;
}
User user = new User();
user.setId(dto.getId());
user.setEmail(dto.getEmail());
user.setPassword(dto.getPassword());
user.setLogin(dto.getLogin());
user.setAccount(accountMapper.dtoToEntity(dto.getAccountDto()));
return user;
}

@Override
public UserDto entityToDto(User user) {
if (user == null) {
return null;
}
UserDto dto = new UserDto();
dto.setEmail(user.getEmail());
dto.setLogin(user.getLogin());
dto.setPassword(user.getPassword());
dto.setId(user.getId());
dto.setAccountDto(accountMapper.entityToDto(user.getAccount()));
return dto;
}
}

带有@SpringBootApplication的类

package com.example.shop;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ShopApplication implements CommandLineRunner {

public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
System.out.println("Test");
}
}

以及我的服务测试:

package com.example.shop.service;

import com.example.shop.dto.UserDto;
import com.example.shop.entity.User;
import com.example.shop.repository.IRepository;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceImplTest {

private static final Long VALID_ID = 1L;

@Mock
private IRepository<User> repository;

@InjectMocks
private UserServiceImpl service;

@After
public void tearDown() {
repository.deleteAll();
}

@Test
public void get() {
service.get(VALID_ID);

verify(repository, times(1)).get(anyLong());
}

@Test
public void save() {
UserDto dto = createUser();
service.save(dto);

verify(repository, times(1)).save(any());
}

@Test
public void getAll() {
service.getAll();
verify(repository, times(1)).getAll();
}

@Test
public void update() {
UserDto dto = createUser();
service.update(dto);
verify(repository, times(1)).update(any());
}

@Test
public void delete() {
UserDto dto = createUser();
service.delete(dto);

verify(repository, times(1)).delete(any());
}

@Test
public void deleteAll() {
service.deleteAll();

verify(repository, times(1)).deleteAll();
}

private UserDto createUser() {
return new UserDto();
}


}

最佳答案

这段代码有几个问题。首先,您不需要使用服务或组件来注释抽象类。抽象类不能被实例化,因此没有bean。第二:具有泛型的类的 Autowiring 不起作用。一旦你有了几个bean,它就不再是唯一的了。 检查您的类是否已实例化。也许您需要添加@componentscan。

您的测试位于:com.example.shop.service 下,因此它仅扫描此包下的 bean。您应该移动测试或使用 Componentsscan 注释添加 bean

关于java - 注入(inject)的 Bean 在抽象类中为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55228968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com