我尝试测试一个类,其中我发送一个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不是声明类的实例
你对这个问题有什么想法吗?
发布于 2018-04-30 17:38:19
我还会注意到,它也不能用JDK1.8.05来模拟JmsTemplate和ObjectMapper,当我将jdk更改为1.8.74时,效果很好。
我引用了discussion
发布于 2018-04-28 08:56:37
如果您正在使用SpringRunner,则应该将其添加到init方法MockitoAnnotations.initMocks(this);中,因为@InjectMocks将正确地使用MockitoJUnitRunner。
PS。ReflectionTestUtils.setField(jmsProducer, "destinationQueue", DESTINATION); -但是您的字段有另一个名称- destination,而不是destinationQueue
https://stackoverflow.com/questions/50063246
复制相似问题