实例化对象上的NullPointerException

时间:2018-11-23 13:14:28

标签: java spring spring-mvc spring-boot spring-test

我正在运行一些测试,由于某种原因,我在下面的行中得到了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();


    }

}

为什么会这样?

1 个答案:

答案 0 :(得分:0)

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


    }

}