无法 运行 Junit case case 。抛出错误 "Actually there were zero interactions with this mock"
Unable to run Junit case case .Throws error "Actually there were zero interactions with this mock"
我正在尝试对 class 进行单元测试。 class如下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=MyConfig.class)
Class MyTest{
@Mock
pirvate JmsTemplate jmsTemplate;
@InjectMocks
private final ProductService productService= new ProductService();
@Test
public void sendItem(){
Item i = new Item();
i.name("xyz");
productService.send(i)
verfity(jmsTemplate).convertAndSend("product.test",i)
}
}
@Configuration
@PropertySource(classpath:application-test.properties)
class MyConfig{
@Bean
ProductService productService(){
return new ProductService();
}
@Bean
JmsTemplate jmsTemplate(){
return new JmsTemplate();
}
}
resources folder under test package has
application.properties, contents of it are
spring.profiles.active=test
And application-test.properties has
queue.name=product.test
我的产品服务class如下
class ProductService{
@Autowired
JmsTemplate jmsTemplate;
@Value("${queue.name}")
private String queue;
public void send(Item i){
jmsTemplate.convertAndSend(queue,i)
}
}
当我运行得到上面的测试用例时,
我想要但没有调用 mockito,
实际上,与此模拟的交互为零。
但是传递给 convertAndSend 方法的参数匹配
谁能提出一些解决方案。
您注入到测试中的 bean 似乎未被 Spring 管理。那这个呢?
@MockBean
private JmsTemplate jmsTemplate;
@Autowired
private ProductService productService;
我正在尝试对 class 进行单元测试。 class如下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=MyConfig.class)
Class MyTest{
@Mock
pirvate JmsTemplate jmsTemplate;
@InjectMocks
private final ProductService productService= new ProductService();
@Test
public void sendItem(){
Item i = new Item();
i.name("xyz");
productService.send(i)
verfity(jmsTemplate).convertAndSend("product.test",i)
}
}
@Configuration
@PropertySource(classpath:application-test.properties)
class MyConfig{
@Bean
ProductService productService(){
return new ProductService();
}
@Bean
JmsTemplate jmsTemplate(){
return new JmsTemplate();
}
}
resources folder under test package has
application.properties, contents of it are
spring.profiles.active=test
And application-test.properties has
queue.name=product.test
我的产品服务class如下
class ProductService{
@Autowired
JmsTemplate jmsTemplate;
@Value("${queue.name}")
private String queue;
public void send(Item i){
jmsTemplate.convertAndSend(queue,i)
}
}
当我运行得到上面的测试用例时,
我想要但没有调用 mockito, 实际上,与此模拟的交互为零。 但是传递给 convertAndSend 方法的参数匹配 谁能提出一些解决方案。
您注入到测试中的 bean 似乎未被 Spring 管理。那这个呢?
@MockBean
private JmsTemplate jmsTemplate;
@Autowired
private ProductService productService;