Spring data and junit

痴心易碎 提交于 2020-01-17 09:49:04

问题


i have a problem where i want to test spring data with junit. I use spring data jpa with junit 4 :

this is my test class

 @ContextConfiguration(locations={"classpath*:applicationContext.xml"})
    @Transactional
    @RunWith(SpringJUnit4ClassRunner.class)
    public class EquipementServiceTest1 {

       @Autowired
        IEquipementService equipmentservice;
        @Before
        public void setUp() throws Exception {
            equipmentservice=new EquipementService();
        }

        @Test
        public void testValidateEquipement() {
           equipementEntity e =new equipementEntity();
           e.setCode("10");
           e.setLibelle("yyyy");
           equipmentservice.validateEquipement(e);
        }

        @Test
        public void testGetListEquipement() {
            List<equipementEntity> list=new ArrayList<equipementEntity>();
            list=   equipmentservice.getListEquipement();
                assertEquals(list.size(), 2);`enter code here`
        }

        @Service
    public class EquipementService implements IEquipementService {

        @Autowired
        EquipementRepository equipementRepository;`enter code here

        @Override
        public List<equipementEntity> getListEquipement() {
            return equipementRepository.findAll();
        }

i have a null pointer exception thank you very much


回答1:


You have a lot of stuff going on here. You autowire EquipmentService but you don't specify the configuration, so the service is not being autowired. Even if it was, you reset it in the setUp method. The new equipmentservice does not have repository set, so NullPointerException is probably thrown at the point equipmentservice tries to call repository.

This is pretty easy to resolve using @ContextConfiguration and Mockito mock objects ( I am assuming repository is autowired)

  1. For you test class add @ContextConfiguration annontation.
  2. Add inner static configuration class to your test
  3. In the configuration make repository a mock object

    @Transactional
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    public class EquipementServiceTest1 {
           @Autowired
            IEquipementService equipmentservice;
            @Before
            public void setUp() throws Exception {
               //remove this 
               // equipmentservice=new EquipementService();
            }
    
            @Test
            public void testValidateEquipement() {
               equipementEntity e =new equipementEntity();
               e.setCode("10");
               e.setLibelle("yyyy");
               equipmentservice.validateEquipement(e);
            }
    
            @Test
            public void testGetListEquipement() {
                List<equipementEntity> list=new ArrayList<equipementEntity>();
                list=   equipmentservice.getListEquipement();
                    assertEquals(list.size(), 2);`enter code here`
            }
    
            @Service
        public class EquipementService implements IEquipementService {
    
            @Autowired
            EquipementRepository equipementRepository;`enter code here
    
            @Override
            public List<equipementEntity> getListEquipement() {
                return equipementRepository.findAll();
            }
       }
    @Configuration
    public static class Config {
    @Bean
    public IEquipementService equipmentservice {
       return new equipmentService();
    }
    
    @Bean
    public EquipmentRepository equipmentRepository {
      return Mockito.mock(EquimentRepository.class);
    }
    
    }
    

This obviously has not been compiled. Also equipmentRepository is a mock, so the call to getListEquipment will return empty List. If you want it to return something you can specify it using Mockito.when method for example. Please not, that this is a Unit Test and it is testing EquipmentService, not EquipmentRepository. If you want to test the functionality of EquipmentRepository you would need to create another Unit test and configure it accordingly.



来源:https://stackoverflow.com/questions/32978956/spring-data-and-junit

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