Spring and junit Testing
1. We know that both J2SE and android have junit tests. Using junit can help you easily test the code. In my previous blog, I also wrote some junit test examples for J2SE. Today I will give a little talk about junit in Spring.
Jar packages required for this Spring test:
Spring-test-3.2.0.RELEASE.jar
2. Relationship between Spring and Junit
The left side uses the traditional method, that is, the general J2SE method to test the code, which may cause some problems: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + release/release + release/rsbPBy6GjPC9zdHJvbmc + release + 7XEx + m/release + 7PwsPmPC9zdHJvbmc + PC9wPgo8cD48c3Ry B25nPjxicj4KPC9zdHJvbmc + PC9wPgo8cD48c3Ryb25nPjMuvPK1pbXEsLjA/aO6PC9zdHJvbmc + volume "brush: java;"> package cn. wwh. www. spring. bbb. test;/*** function of the class: *** @ author yiye Binzhou * @ version 1.0 * @ Creation Time: 03:22:26 */public class HelloWorld {public void show () {System. out. println ("Hello World! This is my first Spring! ");}}
Test Case:
Package cn. wwh. www. spring. bbb. test; import org. junit. test; import org. junit. runner. runWith; import org. springframework. beans. factory. beanFactory; import org. springframework. beans. factory. annotation. autowired; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner;/*** function: test the function of writing a test framework in Spring. *** @ author: @ version 1.0 * @ Creation Time: 10:19:41 * // indicates to start the Spring container and run junit in the Spring container @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfigurationpublic class SpringtestTest {// automatically assemble Spring container @ Autowiredprivate BeanFactory factory; @ Testpublic void testBeanFactory () throws Exception {HelloWorld world = factory. getBean ("helloWorld", HelloWorld. class); world. show ();}}
SpringtestTest-context.xml configuration file:
Note:(1). @ RunWith (SpringJUnit4ClassRunner. class)
@ ContextConfiguration
@ Autowired
@ Test
These are test labels.
(2). @ ContextConfiguration find the configuration file, which is found from the current path by default.
If you set the file name = Current Test class name-context. xml, you can find in the current path. You can omit the above SpringtestTest-context.xml, the test class is: SpringtestTest. java
If it is not written in accordance with the conventions, the test should be written in the format of the loading path:
@ ContextConfiguration ("classpath: cn/wwh/www/spring/bbb/test/SpringtestTest-context.xml ")