简体   繁体   English

Apache Camel 中的 Junit Mock JDBC 存储库

[英]Junit Mock JDBC Repository in Apache Camel

I am trying to mock the jdbc repository in camel but it is still calling the db calls without mocking.Please help me to correct it.我正在尝试在骆驼中模拟 jdbc 存储库,但它仍然在不模拟的情况下调用 db 调用。请帮助我纠正它。

Route:路线:

@Component
public class Router10 extends RouteBuilder {

    @Autowired
    CustomerRepository repository;

    @Override
    public void configure() throws Exception {

        from("file:src/main/resources/in?delete=true").routeId("route1").process((exchange) -> {
            List<Customer> names = repository.findAll();
            System.out.println("names:"+names.size());
            exchange.getOut().setBody(names);
        }).split(simple("${body}")).to("direct:process1");

        from("direct:process1").routeId("process1").process((exchange) -> {
            exchange.getOut().setBody(exchange.getIn().getBody(Customer.class).toString());
        }).to("file:src/main/resources/out?fileName=test_out.txt&fileExist=Append");

    }
}

JUnit:单位:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@SpringBootTest(classes = Router10.class)
@ActiveProfiles("test")
@EnableAutoConfiguration
public class RouterTest2 extends AbstractTestExecutionListener {

    @Autowired
    public CustomerRepository repository;

    @Override
    public void beforeTestClass(TestContext testContext) {
         List<Customer> list = new ArrayList<>(); 
         list.add(new Customer("F1","L1"));
         list.add(new Customer("F2","L2"));
        Mockito.when(repository.findAll()).thenReturn(list);
    }


    @Test
    public void test() throws Exception {
        Thread.sleep(2000);
        File outDir = new File("src/main/resources/out");
        Assert.assertTrue(outDir.isDirectory());
    }    
}

You can create a simple JavaBean class and replace the inline processor in your route with it.您可以创建一个简单的 JavaBean 类并用它替换路由中的内联处理器。 From there you can simply mock the real bean.从那里你可以简单地模拟真正的 bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM