Spring Boot单元测试服务层如何返回Page内容?如何用一些值模拟这些数据,然后进行测试?
需要测试的服务:
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CampaignReadServiceImpl02 {
private final CampaignRepository campaignRepository;
public Page<Campaign> getAll(int page, int size) {
Pageable pageable = PageRequest.of(page, size);
Page<Campaign> pages = campaignRepository.findAll(pageable);
return pages;
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试中模拟数据的类
@Slf4j
@ExtendWith(MockitoExtension.class)
public class CampaignReadServiceTest {
@Mock
private CampaignRepository campaignRepository;
private CampaignReadServiceImpl02 campaignReadServiceImpl02;
@BeforeEach
public void beforeEach() {
campaignReadServiceImpl02 = new CampaignReadServiceImpl02(campaignRepository);
}
@Test
public void testGetAll02() {
log.info("Testing get all campaigns method");
//this need to have content data inside of page.getContent(), need …Run Code Online (Sandbox Code Playgroud) 就像标题所说,我需要在单击后禁用按钮一段时间,这样它就不可单击,当时间到期时再次启用按钮,这样它就可以单击。
Button(
onClick = {},
modifier = //maybe to add code here?
) {
Text(text = "Click me")
}
Run Code Online (Sandbox Code Playgroud)
如果可以在Modifier中添加代码就太好了,那就太好了!因为这样它就可以重用,并且不仅可以用于 Button,还可以用于其他组件。