小编Mic*_*son的帖子

使用Spring和Mockito测试RabbitMQ

尝试使用RabbitTemplate进行Spring JUnit运行测试,并使用Mockito存根服务类注入侦听器.试图验证与模拟的交互.通过我见过的例子,我认为这是可能的.RabbitMQ正在运作.登录仪表板时,我可以在那里看到消息.能够使用独立控制台应用程序使用消息.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/servlet-context.xml", "classpath:/spring/root-context.xml", "classpath:rabbitlistener-context-test.xml"})
public class ReceiveOrderQueueListenerTest {

    @Mock
    private ReceiveOrderRepository mockRepos;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    SimpleMessageListenerContainer  listenerContainer;

    @InjectMocks
    @Autowired
    ReceiveOrderQueueListener receiveOrderQueueListener;


    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testAbleToReceiveMessage() {
        RequestForService rfs = new RequestForService();
        rfs.setClaimNumber("a claim");
        rabbitTemplate.convertAndSend("some.queue", rfs);
        verify(mockRepos).save(new OrderRequest());
    }

} 
Run Code Online (Sandbox Code Playgroud)

然后兔子听众配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
          http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">

     <rabbit:connection-factory id="rabbitConnectionFactory" host="XXXXXXXXX.com"   username="test" password="test" /> 
 <!--    <rabbit:connection-factory id="rabbitConnectionFactory" host="localhost" username="guest" …
Run Code Online (Sandbox Code Playgroud)

java junit spring mockito rabbitmq

9
推荐指数
1
解决办法
5066
查看次数

为什么spring boot的DataJpaTest扫描@Component

确信没有人问过这个问题,但是通过阅读 Spring 文档和测试实用程序,我发现了这个注释,并认为我会开始使用它。通读小字,我读到:

常规的@Component bean 不会加载到 ApplicationContext 中。

这听起来不错,我什至喜欢使用 H2 的想法,除了我发现我想要使用的实体具有目录和模式修饰符,而默认的 H2 我不知道如何支持它。我为测试分支创建了一个 H2 数据源并使用它并覆盖替换。我结束了

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=ABCH2Congfiguration.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class StatusRepositoryTest {

}
Run Code Online (Sandbox Code Playgroud)

但是我的测试失败了原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有符合类型的bean。这导致:创建名为“customerServiceImpl”的 bean 时出错:依赖项不满足。

但是 customerServiceImpl 是这个 bean:

@Component
public class CustomerServiceImpl  implements CustomerService {
}
Run Code Online (Sandbox Code Playgroud)

那就是@Component。DataJpaTest 的细则说它不加载@Components。为什么它会这样做,从而导致测试失败?

正如凯尔和尤金在下面问的那样,剩下的就是:

package com.xxx.abc.triage;
@Component
public interface CustomerService {
}

Configuration
@ComponentScan("com.xxx.abc")
@EnableJpaRepositories("com.xxx.abc")
//@Profile("h2")
public class ABMH2Congfiguration {

    @Primary
    @Bean(name = "h2source")
    public DataSource dataSource() {
        EmbeddedDatabase build = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).setName("ABC").addScript("init.sql").build();
        return build;
    }

    @Bean
    public JpaVendorAdapter …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot spring-boot-test

9
推荐指数
1
解决办法
1416
查看次数

我该如何使用powermockito verifyNew?

遇到麻烦.我过去曾经使用过Powermockito.通常这很顺利.我想我会发布我的问题,而不是继续通过例子翻找.因此,目标是验证对类的新调用.我不认为这是powermockito最受欢迎的功能.这是测试:

import static org.powermock.api.mockito.PowerMockito.verifyNew;
import static org.powermock.api.mockito.PowerMockito.whenNew;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassUnderTest.class)
public class VerifyNewTest {

  ClassUnderTest myClassUnderTest = new ClassUnderTest();

  @Before
  public void setUp() throws Exception {
  }

  @Test
  public void test() throws Exception {
    whenNew(Collaborator.class).withNoArguments().thenReturn(new Collaborator());
    myClassUnderTest.doSomething();
    verifyNew(Collaborator.class).withNoArguments();
  }

}
Run Code Online (Sandbox Code Playgroud)

并说上课

public class ClassUnderTest {

  public void doSomething() {
    new Collaborator();
  }
}


public class Collaborator {

}
Run Code Online (Sandbox Code Playgroud)

我的目标是尽可能简化.我想我可以添加一些模拟对象并存在一些行为.无论如何,我明白了.

org.mockito.exceptions.misusing.UnfinishedStubbingException:  Unfinished stubbing detected here:
    -> at org.powermock.api.mockito.internal.invocationcontrol. MockitoNewInvocationControl.expectSubstitutionLogic(MockitoNewInvocationControl.java:65)
Run Code Online (Sandbox Code Playgroud)


例如,thenReturn()可能会丢失.正确存根的例子:

when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Run Code Online (Sandbox Code Playgroud)

提示:
1.缺少thenReturn()
2.你试图找到最终方法,你顽皮的开发人员!

powermock

6
推荐指数
1
解决办法
5833
查看次数