【问题标题】:NullPointerException : JUnit test for RepositoryNullPointerException:存储库的 JUnit 测试
【发布时间】:2020-11-20 05:22:27
【问题描述】:

这样为 Repository 编写 JUnit 测试用例是否正确?同样在运行它时,我在 Mockito.when 行得到 NullPointerException... 这是我的测试课:

@ContextConfiguration(locations = { "classpath*:resources/invoices-context.xml",
        "classpath*:resources/invoices-int-schema.xml" })
public class RoomRepositoryTest {

    @Autowired
    RoomRepository roomRepository;

    private RoomEntity roomEntity;

    @Test
    public void findRommByDateTest() throws ParseException {
        String startDate = "27-07-2020";
        sDate = new SimpleDateFormat("dd-mm-yyyy").parse(startDate);
        String endDate = "28-07-2020";
        eDate = new SimpleDateFormat("dd-mm-yyyy").parse(endDate);
        String roomType = "SINGLE";

        roomEntity = new RoomEntity();
        roomEntity.setRoomId(1);
        roomEntity.setRoomPrice(6000);
        roomEntity.setRoomStatus("AVAILABLE");
        roomEntity.setRoomType("SINGLE");
        Mockito.when(
                roomRepository.findRoomByDate(Mockito.any(Date.class), Mockito.any(Date.class), Mockito.anyString()))
                .thenReturn(roomEntity.getRoomId());
        int id = roomRepository.findRoomByDate(Mockito.any(Date.class), Mockito.any(Date.class), Mockito.anyString());
        assertEquals(1, id);
    }
}

【问题讨论】:

  • 为什么不应该呢?没有@RunWith(SpringRunner.class),所以基本上所有的测试都会在你的测试中被忽略,因此会出现空指针异常。
  • 即使添加了@RunWith(SpringRunner.class),我仍然收到异常
  • 它不能是null 否则你的测试会因为一个错误告诉你它不能被自动连接而中断。另外,如果您自动装配 bean,为什么要使用 Mockito?如果您想模拟为什么要使用自动装配的字段,那么只需模拟它(这将使您的测试无用,因为您没有测试任何东西)。
  • 我是新手,这是我第一次为存储库编写 JUnit。那么,除了使用 Mockito 之外,我还可以使用什么替代方式?
  • 要么编写集成测试(使用数据库等),要么不为存储库编写测试,模拟它并检查是否返回结果只是模拟框架的测试,而不是你的存储库。

标签: spring-boot junit spring-data-jpa


【解决方案1】:

您可以使用带有一些注释的嵌入式数据库设置,而不是模拟数据库获取功能。它帮助我们有效地测试查询。如果您首先使用内存数据库(嵌入式),则必须在表中添加(保存)条目。然后你的查询函数应该获取数据。借助此功能,您可以轻松调试空指针异常。

例如:

@SpringBootTest
@AutoConfigureTestDatabase
@ContextConfiguration(locations = { "classpath*:resources/invoices-context.xml",
        "classpath*:resources/invoices-int-schema.xml" })
public class RoomRepositoryTest {

    @Autowired
    RoomRepository roomRepository;

    private RoomEntity roomEntity;

    @Test
    public void findRommByDateTest() throws ParseException {
        String startDate = "27-07-2020";
        sDate = new SimpleDateFormat("dd-mm-yyyy").parse(startDate);
        String endDate = "28-07-2020";
        eDate = new SimpleDateFormat("dd-mm-yyyy").parse(endDate);
        String roomType = "SINGLE";

        roomEntity = new RoomEntity();
        roomEntity.setRoomId(1);
        roomEntity.setRoomPrice(6000);
        roomEntity.setRoomStatus("AVAILABLE");
        roomEntity.setRoomType("SINGLE");

        //adding data for testing  
        roomRepository.save(roomEntity)
        
        //test the actual function
        int id = roomRepository.findRoomByDate(sDate,eDate);
        assertEquals(1, id);
   
}

【讨论】:

  • 我按照您建议的方式进行操作,但出现 IllegalStateException : Failed to load Application Context
  • 如果您提供了不正确的查询也会发生这种情况,您可以在此处粘贴确切的错误消息
  • 对我来说问题是,存储库类和主类在不同的模块中。假设存储库位于 com.booking.model 包中的模块 A 中。主类位于 com.booking.service 包中的模块 B 中。
  • 我是新手,无法了解如何在这种情况下正确使用 i@SpringBootTest。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 2020-12-07
  • 1970-01-01
  • 2011-11-04
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多