Home > Java > javaTutorial > body text

How to integrate Junit between Java and Spring?

WBOY
Release: 2023-04-21 11:13:07
forward
1245 people have browsed it

    1 Problems and solutions in the test class

    1.1 Problem

    • In the test class, every Each test method has the following two lines of code:

      • ##ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

      • IAccountService as = ac.getBean("accountService",IAccountService.class);

    • The function of these two lines of code is to obtain the container. If you do not write it, it will directly prompt empty Pointer exception. So it cannot be deleted easily.

    1.2 Analysis of solution ideas

    • In view of the above problems, what is needed is that the program can

      automatically create containers.

    • junit cannot know whether the spring framework is used and cannot create a spring container, but junit exposes an annotation that can replace its runner.

    • You need to rely on the runner provided by the spring framework. You can read the configuration file (or annotations) to create the container. You only need to tell it where the configuration file is.

    2 Configuration steps

    2.1 Step 1: Copy the necessary jar package for integrating junit to the lib directory

    • This It should be noted that when importing a jar package, you need to import a jar package of aop in spring.

    How to integrate Junit between Java and Spring?

    2.2 Step 2: Use @RunWith annotation to replace the original runner

    @RunWith(SpringJUnit4ClassRunner.class)
    public class AccountServiceTest {
    }
    Copy after login

    2.3 Step 3: Use @ContextConfiguration Specify the location of the spring configuration file

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations= {"classpath:bean.xml"})
    public class AccountServiceTest {
    }
    Copy after login

    • @ContextConfiguration Note:

      • locations attribute: used to specify the location of the configuration file. If it is on the classpath, you need to use classpath: to indicate

      • classes attribute: used to specify the annotated classes. When not using xml configuration, you need to use this attribute to specify the location of the annotation class.

    2.4 Step 4: Use @Autowired to inject data into the variables in the test class

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations= {"classpath:bean.xml"})
    public class AccountServiceTest {
     
        @Autowired
        private IAccountService as ;
    }
    Copy after login

    3 Reasons for not assigning the test class to xml

    • First: When a bean is configured in xml and spring loads the configuration file to create the container, the object will be created.

    • Second: The test class is only used when testing functions. In the project, it does not participate in the program logic and will not solve the demand problems, so after it is created, there is no use. Then storing it in the container will cause a waste of resources.

    The above is the detailed content of How to integrate Junit between Java and Spring?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!