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 solve the problem that Autowired can not be injected into bean in Test class

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to solve the problem that Autowired can not be used to inject bean into Test class. I think it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

You cannot use Autowired to inject bean in a Test class

I took a good look at the notes about Spring this afternoon.

Using the AutoWired annotation in the test class has never been able to get the Bean, and a null pointer exception has been reported when the method is called.

Using the getBean method of ApplicationContext, we can get the Bean again, which proves that our Bean annotation is valid, and the problem should be in AutoWired. I have tried using AutoWired in other classes and found that it works.

The problem should be in the Test class. After searching for a long time, I finally found the problem!

The test unit I use myself in the test class is @ RunWith (BlockJUnit4ClassRunner.class). The correct one is to use the test unit @ RunWith (SpringJUnit4ClassRunner.class) in Spring-test.

Everyone must be careful in writing the code. Otherwise more time will be wasted.

Use autowired injection prompt Could not autowire. Test in the package. No beans of 'xxx' type found.

Tried various methods on the Internet, and finally found out

Replace autowired annotations with Resource annotations perfectly

This is the end of this article on "how to solve the problem that Autowired can not be injected into bean in Test classes". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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