简体   繁体   中英

NullPointerException on an instantiated object

I am running some test and for some reason, I am getting a nullpointerexception on the line below:

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


    }

}

Why might this be?

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


    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM