1 回答

TA貢獻1808條經驗 獲得超4個贊
您可以使用服務層模擬DAO 層@RunWith(MockitoJUnitRunner.class)
組件來進行單元測試。你不需要它。SpringRunner.class
? ? @RunWith(MockitoJUnitRunner.class)
? ? public class GatewayServiceImplTest {
? ? ? ? @Mock
? ? ? ? private GatewayRepository gatewayRepository;
? ? ? ? @InjectMocks
? ? ? ? private GatewayServiceImpl gatewayService;
? ? ? ? @Test
? ? ? ? public void create() {
? ? ? ? ? ? val gateway = GatewayFactory.create(10);
? ? ? ? ? ? when(gatewayRepository.save(gateway)).thenReturn(gateway);
? ? ? ? ? ? gatewayService.create(gateway);
? ? ? ? }
? ? }
您可以用于@DataJpaTest與DAO 層進行集成測試
? ? @RunWith(SpringRunner.class)
? ? @DataJpaTest
? ? public class GatewayRepositoryIntegrationTest {
? ? ? ? @Autowired
? ? ? ? private TestEntityManager entityManager;
? ? ? ? @Autowired
? ? ? ? private GatewayRepository gatewayRepository;
? ? ? ? // write test cases here? ? ?
? ? }
添加回答
舉報