Spring integrates testng and JUnit

Source: Internet
Author: User
Tags config testng

Consolidate JUnit for testing:

JUnit has natural support for spring, and just add two annotation to start the spring context, the sample code is as follows:

@ContextConfiguration ("/meta-inf/spring/integration/inbound-gateway-config.xml")
@RunWith ( Springjunit4classrunner.class) Public
class Inboundgatewaytests {

	@Autowired
	private Simplewebserviceinboundgateway Gateway;
}



Integrate testng for testing:

Unlike JUnit, testng does not provide @runwith annotations. TestNG test cases need to inherit org.springframework.test.context.testng.AbstractTestNGSpringContextTests or Org.springframework.test.context.testng.AbstractTestNGSpringContextTests to start Springcontext, the sample code is as follows:

@ContextConfiguration (locations={"/web-inf/accountcontext.xml"}) public 
class Testdao extends abstracttestngspringcontexttests {
    @Autowired
    jdbctemplate jdbctemplate;

    @Test public
    void Init () throws Servletexception {
        try {
            jdbctemplate.execute ("CREATE Table Contacts (name varchar (), email varchar, phone varchar) ");
            List List = Jdbctemplate.queryforlist ("SELECT * from Contacts");
        } catch (Exception e) {
            e.printstacktrace (System.out);}}
}



The settings for the @contextconfiguration parameter are explained below:
1. Do not specify a context file, @ContextConfiguration
Spring's contextloader will determine if the context needs to be loaded, and if necessary, the default loader (Genericxmlcontextloader) will generate a spring context configuration file name based on the use case class name, for example, If the test case is com.example.MyTest, Genericxmlcontextloader loads the context from "Classpath:/com/example/mytest-context.xml".
2. Specify multiple context files, such as @contextconfiguration ("/meta-inf/spring/integration/inbound-gateway-config.xml")
3. Specify multiple context files, such as @contextconfiguration (locations={"/applicationcontext.xml", "/applicationcontext-test.xml"})
Contextloader will load the above files from the root directory of the classpath.
4. The impact of inheritance on context file loading, the use of inheritlocations.
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"/base-context.xml"})
Public Class Basetest {
    //class body ...
}

@ContextConfiguration (locations={"/extended-context.xml"}) public
class Extendedtest extends Basetest {
    // Class body ...
}

In the above code extendedtest will load "/base-context.xml" and "/extended-context.xml" two files, because the default value of Inheritlocations is true.

From the above explanation, we can see that the spring context file is located based on Classpath. Under Eclipse, the classpath settings are often different from the build environment, how to set the context file in the build environment to @contextconfiguration. The steps are as follows: Click Run->run configurations ... Select the Run configuration item for the test case you want to run, such as incontainertests, then click User Entries, then click Advanced, select Add External Folder in the pop-up window, and click OK to select any directory. You can configure the parameters in the @contextconfiguration based on the location of the context file relative to the directory.

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.