Spring单元测试技巧

来自ling
跳转至: 导航搜索

具有启动依赖

    @Test
    public void testAnnotation() {
        ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext("beans/dubbo/demoServiceProvider.xml");
        providerContext.start();
        try {
            ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext("beans/dubbo/demoserviceCustomer.xml");
            consumerContext.start();
            try {
                DemoService demoService = (DemoService) consumerContext.getBean("demoService");
                String hello = demoService.sayHello("world");
                assertEquals("Hello world", hello);
            } finally {
                consumerContext.stop();
                consumerContext.close();
            }
        } finally {
            providerContext.stop();
            providerContext.close();
        }
    }