我正在运行一些测试,由于某种原因,我在下面的行中得到了nullpointerexception:

“ 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();


    }

}

07-26 08:50