gpt4 book ai didi

java - 实例化对象上的 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:16 24 4
gpt4 key购买 nike

我正在运行一些测试,由于某种原因,我在下面的行中收到一个空指针异常:

“friendService.save(friend1);”

    @DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class ServiceTests {

@Autowired
FriendService friendService;


@Test
public void testCreateReadDelete() {


Friend friend1 = new Friend("Gordon", "Moore");

friendService.save(friend1);

Iterable<Friend> friends = friendService.findAll();
Assertions.assertThat(friends).extracting(Friend::getFirstName).containsOnly("Gordon");

friendService.deleteAll();
Assertions.assertThat(friendService.findAll()).isEmpty();


}

}

为什么会这样?

最佳答案

@RunWith(SpringJUnit4ClassRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class ServiceTests {

@Autowired
FriendService friendService;


@Test
public void testCreateReadDelete() {


Friend friend1 = new Friend("Gordon", "Moore");

friendService.save(friend1);

Iterable<Friend> friends = friendService.findAll();
Assertions.assertThat(friends).extracting(Friend::getFirstName).containsOnly("Gordon");

friendService.deleteAll();
Assertions.assertThat(friendService.findAll()).isEmpty();


}

}

关于java - 实例化对象上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53447418/

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