Spring Boot 是否可以在构造函数中调用模拟对象的方法?

uqcuzwp8  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(135)

例如,我有这些类与Sping Boot .我尝试做一个REST API没有数据库和wieh文件作为数据.数据文件如下:
{“人员”:[ {“名字”:“约翰”,“姓氏”:“博伊德”,“地址”:“卡尔弗街1509号,“城市”:“卡尔弗,“邮编”:“97451”,“电话”:“841-874-6512”,“电子邮件”:“jaboyd@email.com“},{“名字”:“雅各布”,“姓氏”:“博伊德,“地址”:”卡尔弗街1509号,“城市”:“卡尔弗,“邮编”:“97451”,“电话”:“841-874-6513”,“电子邮件”:“drk@email.com“}]

@Repository
public class PersonRepository {

    private List<Person> persons;
    private DataLoaderService loaderService;
  
    @Autowired
    public PersonRepository(DataLoaderService loaderService){
        persons = loaderService.convertJsonToPojo("Persons",Person.class);
    }
    
    public List<Person> getAll(){
        return persons;
    }

}

@Service
public class DataLoaderService {

    private JsonFileService jsonFileService;
    private ObjectMapper mapper

    @Autowired
    public DataLoaderService(JsonFileService jsonFileService,ObjectMapper mapper){
        this.jsonFileService = jsonFileService;
        this.mapper = mapper;
    }

    public <T> List<T> convertJsonToPojo(String nodeName, Class <T> classOfT){
         
    }

}

所以,我有一个文件。我如何读取转换为Pojo列表?
当我想模拟测试方法getAll()时,我的列表大小是0。模拟没有给予我值,因为我认为问题是**我在构造函数中初始化了值。**下面是我的测试:

@ExtendWith(MockitoExtension.class)
public class PersonRepositoryTest {
    
    PersonRepository repository;
    
    @Mock
    private DataLoaderService loaderService;
    
    @BeforeEach
    public void setUp() {
        repository = new PersonRepository(loaderService);
    }

    @Test
    public void getAllPersonnesInConstructor() {
        List<Person> mockedList = Arrays.asList(                
                new Person("Paul","Moes","1", "7777", "adresse tour", "Chicago", "pauln@gmail.com"),
                new Person("Eleson","Moc","2", "77777", "ddkdkd", "New York", "eleson@gmail.com")
                );
        doReturn(mockedList).when(loaderService).convertJsonToPojo("persons",Person.class);
        
        List<Person> persons = repository.getAll();

        assertEquals(2,persons.size(),"Expected list size is 2");
        assertEquals(persons,mockedList);
    }

}

如果我使用@Spy,就会出现错误。
当我使用方法getAll()时,没有在构造函数中初始化变量person,而是在方法getAll中初始化变量person,这是可以的,如下所示:

public List<Person> getAll() {
        this.persons = this.dataLoaderService.convertJsonToPojo("persons", Person.class);
        log.debug("persons getALL repository" + persons);
        return this.persons;
}

我能做些什么来测试它呢?
测试初始化构造函数中值的方法。

jgwigjjp

jgwigjjp1#

你在测试中犯了两个错误:
1.您模拟convertJsonToPojo("persons", Person.class),而PersonRepository模拟convertJsonToPojo("Persons", Person.class)。注意"persons"与"Persons"。
1.当PersonRepository在mock上调用convertJsonToPojo方法时,到目前为止还没有stub(doReturn等)任何东西,这就是为什么会得到默认答案,即大小为0的列表。
要使其正常工作,请按如下所示修改测试:

@ExtendWith(MockitoExtension.class)
class PersonRepositoryTest {

    PersonRepository classUnderTest;

    @Mock
    DataLoaderService dataLoaderServiceMock;

    @Test
    void getAllPersonnesInConstructor() {
        List<Person> pojos = List.of(
                new Person("Paul", "Moes", "1", "7777", "adresse tour", "Chicago", "pauln@gmail.com"),
                new Person("Eleson", "Moc", "2", "77777", "ddkdkd", "New York", "eleson@gmail.com")
        );
        when(dataLoaderServiceMock.convertJsonToPojo("Persons", Person.class)).thenReturn(pojos);
        classUnderTest = new PersonRepository(dataLoaderServiceMock);

        List<Person> persons = classUnderTest.getAll();

        assertEquals(persons, persons);
    }

}

现在,存根发生在实际调用convertJsonToPojo之前。
旁注:

相关问题