Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring Integration Junit

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Spring integration Junit how to use", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Spring integration Junit how to use" this article.

After writing the Spring code, we often need to test the correctness of the code, so we need to use unit tests at this time. The version we use here is junit4.

The entry of a program is the main method, but the main method does not exist in junit because the internal principle of junit is that it has a main method inside it, runs the method with @ Test annotation, and then reflects calling the method to complete the test.

The test code that calls the Spring framework:

@ Test public void function () {ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("bean.xml"); AccountDao accountDao1 = applicationContext.getBean ("AccountDao", AccountDao.class); accountDao1.findAll ();}

We found that this is only a query, and the addition, deletion and modification methods have not been tested, but these tests all have duplicate code blocks, which we should extract.

ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("bean.xml"); AccountDao accountDao1 = applicationContext.getBean ("AccountDao", AccountDao.class)

We can define a global variable outside to store the value of accountDao and inject the object through the @ Autowired annotation so that it can be used by every method.

@ Autowiredprivate AccountDao accountDao

But after running like this, a null pointer exception will be burst!

This is because Junit does not recognize the Spring framework by default, so it does not have an internal IOC container, so even if you have the @ Autowired annotation, it does not know where to inject data, so there will be this exception.

After analyzing the cause of the problem, we wondered if we could provide an IOC container ourselves, that is,

ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("bean.xml")

Often in real development, software development and software testing are two positions, if the above code is written by development, there is often no problem. But testers may not understand Spring code, so another approach is needed. Fortunately, Spring provides us with the use of integrating Junit.

First introduce the jar coordinates of Spring-test

Org.springframework spring-test 5.1.5.RELEASE test

Spring provides us with a main method, which supports the Spring framework, and we replace Junit's main method with this main.

@ RunWith (SpringJUnit4ClassRunner.class)

With this comment on the class, @ Runwith represents the operator to be replaced, followed by bytecode parameters

Tell Spring the location of the configuration file / configuration class

@ ContextConfiguration (locations = "classpath:bean.xml")

This can be done using Contextfiguration annotations, where locations indicates the location of the configuration file, plus classpath represents the classpath.

At the end of the integration, the test method can be used directly, but there is a version problem here. If you are using Spring5.0 or above, your Junit version must be above 4.12!

Otherwise, it will be exposed.

Java.lang.ExceptionInInitializerErrorat sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance (Constructor.java:423)

This mistake.

After changing the version, you can run the test method normally.

The above is all the contents of the article "how to use Spring Integration Junit". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report