网站系统功能描述,设计制作公司,网站建设属营改增范围吗,semester什么意思afterclass如何允许实例方法作为JUnit BeforeClass行为运行 JUnit允许您在所有测试方法调用之前和之后一次在类级别上设置方法。 但是#xff0c;通过有意设计#xff0c;它们将其限制为仅使用BeforeClass和AfterClass批注的静态方法。 例如#xff0c;以下简单演示演示了典… afterclass 如何允许实例方法作为JUnit BeforeClass行为运行 JUnit允许您在所有测试方法调用之前和之后一次在类级别上设置方法。 但是通过有意设计它们将其限制为仅使用BeforeClass和AfterClass批注的静态方法。 例如以下简单演示演示了典型的Junit设置 package deng.junitdemo;import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;public class DemoTest {Testpublic void testOne() {System.out.println(Normal test method #1.);}Testpublic void testTwo() {System.out.println(Normal test method #2.);}BeforeClasspublic static void beforeClassSetup() {System.out.println(A static method setup before class.);}AfterClasspublic static void afterClassSetup() {System.out.println(A static method setup after class.);}
} 并应产生以下输出 A static method setup before class.
Normal test method #1.
Normal test method #2.
A static method setup after class. 在大多数情况下此用法都可以但是有时候您想使用非静态方法来设置测试。 稍后我将向您展示更详细的用例但现在让我们看看如何首先使用JUnit解决这个顽皮的问题。 我们可以通过使测试实现一个提供before和after回调的Listener来解决此问题并且需要挖掘JUnit来检测此Listener来调用我们的方法。 这是我想出的解决方案 package deng.junitdemo;import org.junit.Test;
import org.junit.runner.RunWith;RunWith(InstanceTestClassRunner.class)
public class Demo2Test implements InstanceTestClassListener {Testpublic void testOne() {System.out.println(Normal test method #1);}Testpublic void testTwo() {System.out.println(Normal test method #2);}Overridepublic void beforeClassSetup() {System.out.println(An instance method setup before class.);}Overridepublic void afterClassSetup() {System.out.println(An instance method setup after class.);}
} 如上所述我们的监听器是一个简单的合同 package deng.junitdemo;public interface InstanceTestClassListener {void beforeClassSetup();void afterClassSetup();
} 我们的下一个任务是提供将触发设置方法的JUnit运行器实现。 package deng.junitdemo;import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;public class InstanceTestClassRunner extends BlockJUnit4ClassRunner {private InstanceTestClassListener InstanceSetupListener;public InstanceTestClassRunner(Class? klass) throws InitializationError {super(klass);}Overrideprotected Object createTest() throws Exception {Object test super.createTest();// Note that JUnit4 will call this createTest() multiple times for each// test method, so we need to ensure to call beforeClassSetup only once.if (test instanceof InstanceTestClassListener InstanceSetupListener null) {InstanceSetupListener (InstanceTestClassListener) test;InstanceSetupListener.beforeClassSetup();}return test;}Overridepublic void run(RunNotifier notifier) {super.run(notifier);if (InstanceSetupListener ! null)InstanceSetupListener.afterClassSetup();}
} 现在我们从事业务。 如果我们在测试之上运行它应该会给我们类似的结果但是这次我们使用的是实例方法 An instance method setup before class.
Normal test method #1
Normal test method #2
An instance method setup after class. 一个具体的用例使用Spring Test Framework 现在让我向您展示一个上面的真实用例。 如果使用Spring Test Framework通常会设置一个这样的测试以便可以将测试夹具作为成员实例注入。 package deng.junitdemo.spring;import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;import java.util.List;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration
public class SpringDemoTest {Resource(namemyList)private ListString myList;Testpublic void testMyListInjection() {assertThat(myList.size(), is(2));}
} 您还需要在同一包下的spring xml才能运行 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idmyList classjava.util.ArrayListconstructor-arglistvalueone/valuevaluetwo/value/list/constructor-arg/bean
/beans 非常注意成员实例ListString myList 。 运行JUnit测试时Spring将注入该字段并且可以在任何测试方法中使用它。 但是如果您想一次性设置一些代码并获得对Spring注入字段的引用那么您很不幸。 这是因为JUnit BeforeClass将强制您的方法为静态方法。 如果您将字段设为静态则在测试中无法使用Spring注入 现在如果您是经常使用Spring的用户您应该知道Spring Test Framework已经为您提供了一种处理此类用例的方法。 这是一种使用Spring样式进行类级别设置的方法 package deng.junitdemo.spring;import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;import java.util.List;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;RunWith(SpringJUnit4ClassRunner.class)
TestExecutionListeners(listeners {DependencyInjectionTestExecutionListener.class, SpringDemo2Test.class})
ContextConfiguration
public class SpringDemo2Test extends AbstractTestExecutionListener {Resource(namemyList)private ListString myList;Testpublic void testMyListInjection() {assertThat(myList.size(), is(2));}Overridepublic void afterTestClass(TestContext testContext) {List? list testContext.getApplicationContext().getBean(myList, List.class);assertThat((String)list.get(0), is(one));}Overridepublic void beforeTestClass(TestContext testContext) {List? list testContext.getApplicationContext().getBean(myList, List.class);assertThat((String)list.get(1), is(two));}
} 如您所见Spring提供了TestExecutionListeners批注以允许您编写任何侦听器并且在其中将具有对TestContext的引用该引用具有ApplicationContext以便您获取注入的字段引用。 这行得通但我觉得它不是很优雅。 当您注入的字段已经可以用作字段时它会强制您查找bean。 但是除非您通过TestContext参数否则您将无法使用它。 现在如果您混合了开始时提供的解决方案我们将看到更漂亮的测试设置。 让我们来看看它 package deng.junitdemo.spring;import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;import java.util.List;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;import deng.junitdemo.InstanceTestClassListener;RunWith(SpringInstanceTestClassRunner.class)
ContextConfiguration
public class SpringDemo3Test implements InstanceTestClassListener {Resource(namemyList)private ListString myList;Testpublic void testMyListInjection() {assertThat(myList.size(), is(2));}Overridepublic void beforeClassSetup() {assertThat((String)myList.get(0), is(one));}Overridepublic void afterClassSetup() {assertThat((String)myList.get(1), is(two));}
} 现在JUnit仅允许您使用单个Runner 因此我们必须扩展Spring的版本以插入之前的操作。 package deng.junitdemo.spring;import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import deng.junitdemo.InstanceTestClassListener;public class SpringInstanceTestClassRunner extends SpringJUnit4ClassRunner {private InstanceTestClassListener InstanceSetupListener;public SpringInstanceTestClassRunner(Class? clazz) throws InitializationError {super(clazz);}Overrideprotected Object createTest() throws Exception {Object test super.createTest();// Note that JUnit4 will call this createTest() multiple times for each// test method, so we need to ensure to call beforeClassSetup only once.if (test instanceof InstanceTestClassListener InstanceSetupListener null) {InstanceSetupListener (InstanceTestClassListener) test;InstanceSetupListener.beforeClassSetup();}return test;}Overridepublic void run(RunNotifier notifier) {super.run(notifier);if (InstanceSetupListener ! null)InstanceSetupListener.afterClassSetup();}
} 这应该够了吧。 运行测试将使用以下输出 12:58:48 main INFO org.springframework.test.context.support.AbstractContextLoader:139 | Detected default resource location classpath:/deng/junitdemo/spring/SpringDemo3Test-context.xml for test class [deng.junitdemo.spring.SpringDemo3Test].
12:58:48 main INFO org.springframework.test.context.support.DelegatingSmartContextLoader:148 | GenericXmlContextLoader detected default locations for context configuration [ContextConfigurationAttributes74b23210 declaringClass deng.junitdemo.spring.SpringDemo3Test, locations {classpath:/deng/junitdemo/spring/SpringDemo3Test-context.xml}, classes {}, inheritLocations true, contextLoaderClass org.springframework.test.context.ContextLoader].
12:58:48 main INFO org.springframework.test.context.support.AnnotationConfigContextLoader:150 | Could not detect default configuration classes for test class [deng.junitdemo.spring.SpringDemo3Test]: SpringDemo3Test does not declare any static, non-private, non-final, inner classes annotated with Configuration.
12:58:48 main INFO org.springframework.test.context.TestContextManager:185 | TestExecutionListeners is not present for class [class deng.junitdemo.spring.SpringDemo3Test]: using defaults.
12:58:48 main INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader:315 | Loading XML bean definitions from class path resource [deng/junitdemo/spring/SpringDemo3Test-context.xml]
12:58:48 main INFO org.springframework.context.support.GenericApplicationContext:500 | Refreshing org.springframework.context.support.GenericApplicationContext44c9d92c: startup date [Sat Sep 29 12:58:48 EDT 2012]; root of context hierarchy
12:58:49 main INFO org.springframework.beans.factory.support.DefaultListableBeanFactory:581
| Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory73c6641: defining beans [myList,org.springframework.context.annotation.
internalConfigurationAnnotationProcessor,org.
springframework.context.annotation.internalAutowiredAnnotationProcessor,org
.springframework.context.annotation.internalRequiredAnnotationProcessor,org.
springframework.context.annotation.internalCommonAnnotationProcessor,org.
springframework.context.annotation.
ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
12:58:49 Thread-1 INFO org.springframework.context.support.GenericApplicationContext:1025 | Closing org.springframework.context.support.GenericApplicationContext44c9d92c: startup date [Sat Sep 29 12:58:48 EDT 2012]; root of context hierarchy
12:58:49 Thread-1 INFO org.springframework.beans.factory.support.
DefaultListableBeanFactory:433
| Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory
73c6641: defining beans [myList,org.springframework.context.annotation.
internalConfigurationAnnotationProcessor,org.springframework.
context.annotation.internalAutowiredAnnotationProcessor,org.springframework.
context.annotation.internalRequiredAnnotationProcessor,org.springframework.
context.annotation.internalCommonAnnotationProcessor,org.springframework.
context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy 显然输出在这里没有显示任何有趣的内容但是测试应该在所有声明通过的情况下运行。 关键是现在我们有一种更优雅的方法来调用类级别的测试之前和之后的测试并且它们可以是允许Spring注入的实例方法。 下载演示代码 您可能会从我的沙箱中获得一个正常运行的Maven项目中的演示代码 参考 A程序员杂志博客上的JCG合作伙伴 Zemian Deng提供的beforeClass和afterClass设置增强了Spring Test Framework 。 翻译自: https://www.javacodegeeks.com/2012/10/enhancing-spring-test-framework-with.htmlafterclass