Spring-Context 2: Use the testing framework provided by Spring for testing

Source: Internet
Author: User

The Spring framework is non-invasive, so your code can be completely POJO (plain old java object). You can directly use Junit to complete most unit tests. However, it is difficult to test the integration. At the unit test level, you can mock some dependent objects, but the actual dependent objects are required for integration testing, and these objects are under the control of the Spring container. So how can we conduct integration testing when Spring is introduced? Don't worry. The Spring framework has come up with this for us, and it provides the integrated testing function. Let's take the last simple example for an experiment. First, introduce the dependency on junit and spring-test libraries. <Dependencies> <dependency> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 4.11 </version> <scope> test </scope> </ dependency> <groupId> org. springframework </groupId> <artifactId> spring-test </artifactId> <version> 4.0.2.RELEASE </version> </dependency> </dependencies> the spring-test module is specially designed for use. auxiliary class library for integration testing for spring framework projects. It has the following purposes. Cache the Spring IOC container during testing to improve the speed of integration testing. Provides the dependency injection function for the test instance. Provides Transaction Management in integration testing. Some helper libraries are provided to help developers better compile integration tests. Create an ApplicationTest. java class. The Code is as follows. Package huangbowen.net; import huangbowen.net. service. cinema; import org. junit. test; import org. junit. runner. runWith; import org. springframework. beans. factory. annotation. autowired; import org. springframework. context. applicationContext; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner; import static org. junit. assert. asser TNotNull; @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (classes = {Application. class}) public class ApplicationTest {@ Autowired private ApplicationContext applicationContext; @ Autowired private Cinema cinema; @ Test public void shouldGetCinemaInstance () {Cinema cinema = applicationContext. getBean (Cinema. class); assertNotNull (cinema) ;}@ Test public void shouldGetAutowiredCinema (){ AssertNotNull (cinema);} @ Test public void shouldGetMovieServiceInstance () {assertNotNull (cinema. getMovieService (); assertThat (cinema. getMovieService (), instanceOf (DefaultMovieService. class);} in this example, ApplicationTest has two annotations. @ RunWith (SpringJUnit4ClassRunner. class ). is a custom JUnit runner provided by the Spring TestContext framework. In this way, you can obtain ApplicationContext in the test class, or even directly perform dependency injection, and use the transaction control test method for execution. After @ RunWith (SpringJUnit4ClassRunner. class) is declared, the @ ContextConfiguration annotation is also declared. This annotation is used to tell the test class Spring configuration in this project. Here we pass in the Application. class because Spring bean is configured in this class. Then you can use the powerful @ Autowired function in the test class. We have written three test methods. The first is to get ApplicationContext through the Autowired function, and the second is to get cinema directly through the Autowired function, the third is to verify that MovieService in Cinema is correctly injected.

Related Article

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.