【发布时间】:2021-01-25 15:46:38
【问题描述】:
我只想在真实服务上模拟一个功能,这是通过@Autowired(集成测试)注入的 ->
@SpringBootTest(classes = OrderitBackendApp.class)
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@WithUserDetails
public class OrderResourceIT {
@Autowired
private OrderRepository orderRepository;
...mor Injections and Tests
这里是模拟功能 ->
@BeforeEach
public void initTest() {
MockitoAnnotations.initMocks(this);
order = createEntity(em);
OrderService spy = spy(orderService);
when(spy.priceIsIncorrect(any())).thenReturn(false);
}
但是我尝试过的这个和其他一些事情没有奏效。正确的做法是什么?
【问题讨论】:
-
this and a few other things didn't work到底是什么意思? -
我尝试使用诸如 (@Autowired @InjectMocks) (@Autowired @Mock) (@Autowired @Spy) 之类的注释。我认为这个注释不能与@Autowired 一起使用
-
您的间谍
OrderService是从哪里注入的?这就是我在这里缺少的一点。间谍服务需要进入您的测试系统(sut)是什么? -
OrderService 在测试中被注入,我更新了描述。我希望它现在更容易理解。