Apache Camel中的Junit Mock JDBC存储库

时间:2018-02-20 11:18:17

标签: java junit apache-camel mockito

我正在尝试在camel中模拟jdbc存储库,但它仍在调用db调用而不进行mocking。请帮我纠正它。

路线:

@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());
    }    
}

1 个答案:

答案 0 :(得分:1)

您可以创建一个简单的JavaBean类,并用它替换路由中的内联处理器。从那里你可以简单地模拟真正的豆。