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

What if the springBoot Junit test case appears @ Autowired does not work?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about what happens when @ Autowired doesn't work in springBoot Junit test cases. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

SpringBoot Junit test case appears @ Autowired does not take effect

Prerequisites:

1. Add supported annotations to the test class

You can get an instance of the container in spring and automatically inject the object if @ Autowired is configured.

@ RunWith (SpringJUnit4ClassRunner.class) @ SpringBootTest (classes = Application.class) / / where Application is the startup class

Pom file add:

Org.springframework.bootspring-boot-testorg.springframeworkspring-test4.3.25.RELEASEcompile2, an error occurred

Java.lang.NoSuchMethodError: org.springframework.util.Assert.state (ZLjava/util/function/Supplier;) V

The occurrence of this kind of error is usually a jar package conflict. Here, the version number of spring-test has been changed from 5.1.11 to 4.3.25 (please refer to the comparative spring-context version). Errors will also be prompted when repeated references are made, so you need to pay attention to the introduction!

3, annotation interpretation

@ runWith annotation function:

-@ RunWith is an operator

-@ RunWith (JUnit4.class) means to run with JUnit4

-@ RunWith (SpringJUnit4ClassRunner.class) to have the test run in the Spring test context to automatically create the Spring application context at the start of the test

-@ RunWith (Suite.class) is a set of tests

SpringTest, combined with other testing frameworks such as JUnit, provides a convenient and efficient testing means. On the other hand, SpringBootTest is re-packaged on top of SpringTest, which adds slice testing and enhances mock capabilities.

4 the first step of how to generate test cases directly through in-class methods on idea

Search the plug-in repository for JunitGenerator V2.0 plug-ins and install

Step two

Configure the build directory for test cases

1. Open File- > Settings

two。 Search junit and find JUnit Generator

The directory generated by Output Path in the 3.Properties tab for the test case is modified to the test directory: ${SOURCEPATH} /.. /.. / test/java/$ {PACKAGE} / ${FILENAME}

4. Switch to the JUnit 4 tab to modify the template for generating test cases, such as class name, package name, etc.

Step three

Right-click to create an automatic test case for the specified method

@ Autowired in Junit is invalid

Today, when you learn spring annotations, use junit to test

Create a Student object in a container using annotations

Then use the @ Autowired annotation for automatic assembly

A null pointer exception occurred

Reason

When the Test method executes, it does not create a container for you, and junit does not know whether you are using spring. If there is no container in the default singleton mode, there will be no @ Autowired automatic assembly.

Solution

1. Import the jar package of Spring Integration Junit

Add dependencies in pom.xml

... Junit junit 4.12 compile org.springframework spring-test 5.0.2.RELEASE...

Note that when using 5.x spring dependencies, the junit version needs to be 4.12 or above

two。 Use an annotation provided by Junit-@ Runwith to replace the original main method with the Spring provided

3. Tell Spring's runner whether the creation of Spring and ioc is based on xml or annotations, and indicate the location, using the following notes

@ ContextConfiguration

Locations: specify the location of the xml file, plus the classpath keyword to indicate that it is under the classpath (for IOC using xml files)

Classes: specify the location of the annotation class (if you use the newly created configuration class instead of the xml file for IOC)

As follows:

@ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (locations = "classpath:applicationContext.xml") public class test010 {@ Autowired Student student; @ Test public void test () {student.say ()

Running result

Hello,Student

Thank you for reading! This is the end of the article on "what if the springBoot Junit test case appears @ Autowired does not work". 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, you can share it 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