ITeye博客

gienah-testing5分钟教程

wing929 · 2008-08-20 15:31 · 其它框架 · junit · Spring · XML · 单元测试 · Bean · Markdown

[size=large]SpringRunner[/size] gienah-testing项目主要包含2个Junit4的运行类: springrunner和springtransactionalrunner 。springrunner负责访问主要项目特性的引擎:bean注入。Springtransactionalrunner继承于springrunner ,允许有标记的事务测试在测试完成时将回滚到测试发生前。 使用这些类,你需要在您的测试类中标记JUnit 4的@ runwith。此外,您必须使用gienah-testing的@Configuration标记来指定Spring的配置文件。看看下面的例子: [code="java"]@RunWith(value = SpringRunner.class) @Configuration(locations = {"resources/spring/spring-beans.xml"}) public class TestUserService { @Test public void someTestMethod() { ... } ... }[/code] 我们现在为业务类写一个测试类,请留意@Configuration标记,你可以使用locations属性来指定spring配置文件的路径,将会获取到spring的上下文.还可以以其它方式来使用@Configuration标记, 但这些将经过审查后再补上。 现在,将通过配置运行类来获取变量,请留意下面的例子: [code="java"]@RunWith(value = SpringRunner.class) @Configuration(locations = {"resources/spring/spring-beans.xml"}) public class TestUserService { @Dependency private SomeBean someBean; @Test public void someTestMethod() { Assert.assertNotNull(this.someBean); } }[/code] 使用@Dependency标记可以使我们有效地使配置文件中bean的”somebean”注入到我们的测试类中,因此字段将会以实例的方式出现在我们的代码中,那么,运行类将查找spring上下文中相对应的指定的bean名称的bean,如果你想装载一个bean使用不同的名称,你可以利用@Dependency标记来为它指定”bean”属性. 另外介绍的特性就是允许使用测试标记在测试完成后使事务自动回滚到测试前。例如,下面的示例: [code="java"]@RunWith(value = SpringRunner.class) @Configuration(locations = {"resources/spring/spring-beans.xml"}) public class TestUserService { @Test @Transactional public void someTestMethod() { // doing some database stuff here... } @Test public void someOtherTestMethod() { // doing some database stuff here... } }[/code] "someTestMethod"方法以事务的方式执行,而"someOtherTestMethod"方法则不会。当"someTestMethod"方法执行完后,事务将使数据库状态回滚到未执行前,你也可以使全部测试类都标记@Transactional属性,所有的测试方法都将在事务中执行 [size=large]配置spring上下文[/size] 在前面的话题中,你在执行中见到@Configuration标记。这个标记有三个属性:locations, provider和loader,下面让我们看看如何去使用它们: •locations(可选择):使用这个属性你可以使用指定的spring配置文件来生成spring上下文 •provider(可选择):使用这个属性允许你指定一个类同样可以使用@Configuration标记去指定locations的属性,这里允许一个特许的locations说明在一个测试类套件中 •loader(可选择):这个属性允许你去指定IcontextLoader的实现并去使用它构成spring上下文.一般情况使用默认配置已经足够. @Configuration标记至少指定locations或者provider其中一个属性,如果没有它们其中的一个,将会使JUnit 4显示出一个error.让我们去看看提供的属性在执行中的实例: [code="java"]@RunWith(value = SpringRunner.class) @Configuration(locations = {"resources/spring/spring-beans.xml"}) public class Test1 { ... } @RunWith(value = SpringRunner.class) @Configuration(provider = Test1.class) public class Test2 { ... } @RunWith(value = SpringRunner.class) @Configuration(provider = Test2.class) public class Test3 { ... }[/code] 注意怎样去构造est3从Test2到Test1经过的流程.这个方式(通过路径获取获取配置)是不平常和不受欢迎的,但我想说明一下这个表现. [size=large]访问spring上下文[/size] 最后,也可以直接获取spring上下文,如果这样,你应该在你的测试中创建一个局部变量和写上一个@Context标记: [code="java"]@RunWith(value = SpringRunner.class) @Configuration(locations = {"resources/spring/spring-beans.xml"}) public class TestUserService { @Context private ConfigurableApplicationContext context; @Test public void someTestMethod() { SomeBean someBean = context.getBean("someBean"); ... } }[/code] 当这样做时,运行类将可以使spring上下文将会自动地注入到你的测试类中,然后你也可以访问它. [size=large]配置文件解释[/size] EO.2版本改变了单元测试类的配置,以springXML配置文件的方式填写.因此, 该项目引进一个新的标签:gienah. 在gienah架构下使用标签,你必须在你的springXML配置文件的头部导入声明, The emboldened text in the XML sample below represents the additions you must add to get access to the tags contained in the gienah namespaces. [code="xml"] ... [/code]标签 这个标签在springXML配置文件中定义一个测试类,看下面的例子去了解通常的情况: [code="xml"] [/code] This approach is equals to mark the someBean1 class attribute with the @Dependency annotation. Latest, using the tag you can inject the application context to the Test class. 上述实例在配置文件中把一个测试类命名为TestSample1,它指定了在"testSomething"方法中将支持事务,而其它方法即不运行在同一个事务中(查看主事务属性和标签),另一个有趣的事情是使用可以定义要注入的bean,这样基本上等同于使用@Dependency标记来标注someBean1类属性,最后,使用标签你可以在测试类中注入应用上下文

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。