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 Junit unit tests and configure data sources in Spring projects

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

Share

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

This article mainly introduces how to use Junit unit testing and configure data sources in the Spring project, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Spring uses Junit to unit test and configure data sources I. problem description

Because the data source in the company project is configured in server.xml in Tomcat, the data source cannot be obtained when using Junit for unit testing.

II. Solutions

Since the project integrates features such as automatic injection of Spring, when using Junit for unit testing, you need to ensure that all the configuration files of Spring can be loaded, and that the data sources connected to the database must be loaded, so you need to configure a separate data source as follows:

Create a new spring_jndi_test.xml

Load configuration file and get Bean in Junit test class

Public class CommonDAOJdbc_StandardTest {private volatile static BeanFactory factory; @ Test public void testGetFirmCanOutBalance () {/ / get Bean CommonDAO commonDAO = (CommonDAO) factory.getBean ("commonDAO"); / / you can call the method in the CommonDAO class} @ Before public void init () {System.out.println ("start loading spring configuration.") ArrayList list = new ArrayList (); list.add ("spring.xml"); / / add the Sprint configuration file to the load list list.add ("Spring_jndi_test.xml"); / / add the data source configuration file for testing to the load list try {factory = new ClassPathXmlApplicationContext (new String [list.size ()]) / / ensure that the custom destruction method of singtleton objects in spring will be executed before the virtual machine exits ((AbstractApplicationContext) factory) .registerShutdownHook ();} catch (Exception e) {e.printStackTrace (); System.out.println ("error loading configuration file" + e) } System.out.println ("end of loading spring configuration.");}}

At this point, the unit test of Junit can be done, and the data source can be obtained.

Of course, if "java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver" appears, then Build Path-> Add Libraries is required. Just introduce the ojdbc package.

Click to enter the official website of Junit

An idea of Spring Database relying on Unit Test

Although we will try our best to ensure the simplicity of the test, many unit tests depend on the test data, especially the database. How to ensure the automaticity, repeatability, independence and professionalism of the test is a thorny problem.

A little thought:

[list] [*] each unit_test prepares its own data, maintains the data in the unit test, sets up the rollback, and maintains the independence of the test.

[*] prepare test data uniformly, import test database before unit test, and set rollback

There are two options.

1. It can be applied to the entire unit test class, added in setup, or modified on the basis of prior data. (because it is rollback, it will not affect other tests.)

two。 Only for specific testMethod, make a combination of [*] in test. Unified data preparation should be able to meet most cases and prepare test data in special cases. [/ list]

There are some problems here:

[*] Unit testing prepares data by itself, which is convenient at the beginning. For a long time, there will be a lot of duplicate data and cluttered data.

[*] prepare the data uniformly, and the test data need to be maintained uniformly to avoid unnecessary errors caused by modifications by different people, but in this way, the test data is separated from the test logic. the person who modifies the data may not understand that the modification may change the expected test results, and errors are inevitable. If everyone is maintained separately, confusion is inevitable and there is a correlation between the data.

[*] how to combine the two methods is also a problem. The initial test data is maintained by itself and maintained uniformly after it is stable, which makes people feel better, but I don't know what other problems there will be.

Thank you for reading this article carefully. I hope the article "how to use Junit unit tests and configure data sources in Spring projects" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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