2 回答

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
對(duì)于集成測(cè)試,請(qǐng)考慮使用如下所示的上下文配置實(shí)例化 bean
@RunWith(SpringRunner.class)
@ContextConfiguration("mockito-config.xml")
詳細(xì)解釋請(qǐng)參考
https://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/
對(duì)于單元測(cè)試,請(qǐng)考慮使用 Mockito 提供的 @Mock 注釋。
在這個(gè)特定的例子中,我們可以模擬 SomeOtherService 如下所示
@RunWith(SpringRunner.class)
public class MainServiceTest {
@InjectMocks
private MainService mainService;
@Mock
private SomeOtherService someOtherService
@Test
public void givenSth_whenSth_doSth() {
// test mainService.mainMethod();
}
}
詳細(xì)解釋請(qǐng)參考以下帖子
http://www.vogella.com/tutorials/Mockito/article.html
http://static.javadoc.io/org.mockito/mockito-core/2.23.0/org/mockito/MockitoAnnotations.html

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以按照 Adam Weidner 的教程使用MockBeanSpring Bean 并使用Spring 擴(kuò)展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(...);
添加回答
舉報(bào)