2 回答
TA贡献1744条经验 获得超4个赞
对于集成测试,请考虑使用如下所示的上下文配置实例化 bean
@RunWith(SpringRunner.class)
@ContextConfiguration("mockito-config.xml")
详细解释请参考
https://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/
对于单元测试,请考虑使用 Mockito 提供的 @Mock 注释。
在这个特定的例子中,我们可以模拟 SomeOtherService 如下所示
@RunWith(SpringRunner.class)
public class MainServiceTest {
@InjectMocks
private MainService mainService;
@Mock
private SomeOtherService someOtherService
@Test
public void givenSth_whenSth_doSth() {
// test mainService.mainMethod();
}
}
详细解释请参考以下帖子
http://www.vogella.com/tutorials/Mockito/article.html
http://static.javadoc.io/org.mockito/mockito-core/2.23.0/org/mockito/MockitoAnnotations.html
TA贡献1886条经验 获得超2个赞
您可以按照 Adam Weidner 的教程使用MockBeanSpring Bean 并使用Spring 扩展AbstractTestNGSpringContextTestTestNG
@TestExecutionListeners(MockitoTestExecutionListener.class)
@SpringBootTest
public class WidgetServiceIntegrationTest extends AbstractTestNGSpringContextTest {
@MockBean private SprockService sprockService;
@MockBean private SpringService springService;
@Autowired WidgetService widgetService;
@Test
public void testSomethingOnWidgetService() {
when(sprockService.makeASprock()).thenReturn(...);
添加回答
举报