Mockito 在模拟服务/控制器时返回空指针



当我尝试运行测试时,它会在userService.setUser(user)上抛出一个空指针;并尝试在调试模式下运行它,它只是将我的模拟启动为空。我错过了什么吗?以前还有其他人遇到过类似的问题吗?我确实尝试在用户服务上使用@spy但没有运气。

//服务 包 com.ecommerce.munna.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public void setUser(User user) {
        userRepository.save(user);
    }
    public List<User> getAllUsers() {
        List<User> userList = new ArrayList<>();
        userRepository.findAll()
                .forEach(userList::add);    //findAll is an interable so convert this into list to send it back
        return userList;
    }
    /*public User getUserByUserNameandPassword(String userName, String password)
    {
        return userRepository.findByIdandPassword(userId,password);
    }*/
}

//测试

package com.ecommerce.munna.user;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
//
@RunWith(MockitoJUnitRunner.class)
class UserServiceTest {
    @Mock
    private TestBuilder testBuilder;
    @Mock
    private User user;
    @Mock
    private  UserRepository userRepository;
    @InjectMocks
    private UserService userService;
    @Test
    void setUser_shouldStoreNewuserInRepository() throws Exception {
        ArrayList<User> userList= new ArrayList<User>();
        user=testBuilder.tetsUserBuilder();
        user.setId(1L);
        userList.add(user);
        user.setId(2L);
        userList.add(user);
        userService.setUser(user);
         when(userRepository.findAll()).thenReturn(userList);

        assertEquals(2,userList.size());
    }
}

测试生成器

package com.ecommerce.munna.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestBuilder {
//    @Autowired User user;
//    @Bean
    public static User tetsUserBuilder(){
        User user=new User.UserBuilder()
                .id(1L)
                .userName("munnaTest")
                .password("testPassword")
                .firstName("munna")
                .lastName("Gunturu")
                .dateOfBirth("06/06/1999")
                .eMail("m@yahoo.com")
                .gender("Male")
                .build();
        return user;
    }
}

欢迎来到堆栈溢出!

任何单元测试树部分都按定义的顺序进行:排列行动断言

您的测试在行为部分之后执行安排部分:

   userService.setUser(user); // act
     when(userRepository.findAll()).thenReturn(userList); // arrange

最后一行她应该是测试方法中的第一行。

相关内容

  • 没有找到相关文章

最新更新