Spring and junit testing and Springjunit Testing

Source: Internet
Author: User

Spring and junit testing and Springjunit 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:

(1). Start Spring for each test,

(2) In this case, the test code is used to manage the Spring container, and the test code with the Spring container management is desired.

There may be other cases. Therefore, the junit Test should be conducted in the method on the right, and the Spring framework diagram is also being verified.


3. Simple case:

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:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="helloWorld" class="cn.wwh.www.spring.bbb.test.HelloWorld"/></beans>

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 ")






Error message for junit testing when setting up spring framework

Incorrect syntax. It should be written as follows:
/* Create a method to instantiate the spring container
* ApplicationContext is just an interface through which we reference spring instances
*/

You missing one *

How to Use junit4 for spring integration with hibernate in java web

Write a test class and then write it in the test class:
@ Test
Public void test (){
ApplicationContext context = new ClassPathXmlApplicationContext ("spring/applicationContext. xml ");
SessionFactory sessionFactory = (SessionFactory) context. getBean ("sessionFactory ");
}
Check whether the corresponding table is generated. If yes, you are successfully integrated. If no error is reported in the junit test, but the table is not generated, it may be that your local dialect does not match your database. You should just change the dialect.
Hope to help you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.