Stack Exchange
Injected Beans are null in abstract class
I have abstract user service where I autowired two beans: Repository and AbstractMapper, but when I run test for that class, all faild with NullPointerException. When I run, for example, save test for that service in dubug, it show me that both beans are null. Anybody had this problem? This is my code: 
AbstractService 
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();
}
}

UserServiceImpl
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);
}
}

Abstract Mapper 
package com.example.shop.dto;

import org.springframework.stereotype.Component;

@Component
public interface AbstractMapper<E, D> {

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

User Mapper: 
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;
}
}

Class with @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");
}
}

And my tests for Service:
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();
}


}


Can you paste those files together with packages and also the file, where @SpringBootApplication resides?

@Andronicus Ok sure, I added packages and imports for that files and my SpringBootApplication class

@FilipW what do you mean by "running test"?

@LppEdd I run test class, I attached that above.

Ofcourse there is nothing injected as basically your test is flawed. Replace @InjectMocks with @Autowired and @Mock with @MockBean. Also the after and before actions are useless as you are using a mock not a real database.

There are several problems with this code. First of all you do not need to annotate the abstract classes with service or component. Abstract classes cannot be instantiated, therefore there is no bean.
Second: autowire of classes having generics wont work. As soon as you have several bean, it wont be unique anymore.
 Checkout if your classes get instantiated. Maybe you need to add @componentscan. 
Your test is located under: com.example.shop.service and therefore it only scans the beans under this package. You should either move your test or add the beans by using the componentscan annotation
