无法运行Junit案例。抛出错误“实际上与此模拟交互为零”

时间:2018-11-10 19:12:49

标签: java junit mockito spring-junit

我正在尝试对课程进行单元测试。班级如下

B::operator=()

我的productService类如下

@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

运行上述测试用例时,

我想获得但不被调用的模仿者,     实际上,与该模拟游戏的互动为零。     但是参数传递给convertAndSend方法匹配 谁能提出一些解决方案。

1 个答案:

答案 0 :(得分:1)

您注入到测试中的bean似乎不是由Spring管理的。那这个呢?

@MockBean
private JmsTemplate jmsTemplate;

@Autowired
private ProductService productService;