spring Javax.持久性.元组,如何模拟数据

qaxu7uf2  于 2023-11-16  发布在  Spring
关注(0)|答案(1)|浏览(91)

我有一个返回List的repository方法,它在后台使用了CriteriaQueryTupleTransformer.TupleImpl.在测试中,我想模拟Repository,而Repository.方法返回预定义的模拟数据。
就像这样:

MyRepository myRepository = mock(MyRepository.class);
List<Tuple> = new ArrayList<>();
Tuple tuple = TupleImpl.Builder() //TupleImpl is private class and has no Factory or Builders
            //.addMockedData()
            //.addMockedData()
            .build();

tuples.add(tuple);
//add more mocked data

when(myRepository.findByIds(any())).thenReturn(tuples);

//Assert business logic that everything 
//went as expected when a specific Tuple structure was returned by repo

字符串
这里我的主要问题是,我需要示例化CriteriaQueryTupleTransformer.TupleImpl,它是任何私有类,我找不到任何Builders或Factory方法来方便创建。

h6my8fg2

h6my8fg21#

我做了你在评论中说的。在这里搜索,让人们看到解决方案的样子。使用Mockito。

private Tuple mockedTuple;
    private List<Tuple> tupleList;

    @Before
    public void setUp() {
        mockedTuple = mock(Tuple.class);

        tupleList = new ArrayList<>();
        tupleList.add(mockedTuple);
    }

    @Test
    public void testTuple() {
        when(mockedTuple.get(anyInt())).thenReturn(1);
        when(repository.something(any())).thenReturn(tupleList);
        
        // assertions
    }

字符串

相关问题