【发布时间】:2018-10-08 08:22:42
【问题描述】:
我尝试测试一个发送 jms 消息但我无法模拟 JmsTemplate 的类
JmsProducer.class :
@Component
public class JmsProducer {
@Autowired
private JmsTemplate jmsTemplate;
@Value("${destination}")
private String destination;
public void send(String message){
jmsTemplate.convertAndSend(destination, message);
}
}
JmsProducerTest.Class :
@RunWith(SpringRunner.class)
public class JmsProducerTest {
private static final String DESTINATION= "example";
private static final String MESSAGE= "message";
@InjectMocks
private JmsProducer jmsProducer;
@MockBean
JmsTemplate jmsTemplate;
@Before
public void init(){
ReflectionTestUtils.setField(jmsProducer, "destinationQueue", DESTINATION);
}
@Test
public void testsend(){
jmsProducer.send(MESSAGE);
verify(jmsTemplate,times(1)).convertAndSend(DESTINATION, MESSAGE);
}
}
当我运行这个测试用例时,它给了我:java.lang.IllegalArgumentException: object is not an instance of declaring class
你对这个问题有什么想法吗?
【问题讨论】:
标签: unit-testing spring-boot junit mockito spring-test