我需要编写一个测试,通过Mockito测试getData(Model model)@Service方法。
@RestController
@RequestMapping("")
public class RestCtrl {
@Autowired
private TestService testService;
@GetMapping
public ModelAndView getData(Model model) {
return testService.getData(model)
}
}
@Service
public class TestService {
@Autowired
private TestServiceRepository testServiceRepository;
public ModelAndView getData(Model model) {
model.addAttribute("data", testServiceRepository.findAll());
return new ModelAndView("index");
}
}
public interface TestServiceRepository extends JpaRepository<TestDTO, Long> {
}
字符串
是否可以在返回ModelAndView的@Service方法上进行写测试?
@SpringBootTest
@ExtendWith(MockitoExtension.class)
class TestServiceClass {
@InjectMocks
private TestService testService;
@Mock
private TestServiceRepository testServiceRepository;
}
型
1条答案
按热度按时间sczxawaw1#
是的,有可能。
您可以使用Mockito通过对
Mockito.mock(ModelAndView.class);
的简单调用来模拟ModelAndView
的示例