• [Spring Unit Testing] Spring Unit Testing with a Java Context


    For example, we want to test against a implemataion:

    package com.example.in28minutes.basic;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.config.ConfigurableBeanFactory;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    @Component
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class BinarySearchImpl {
    
        @Autowired
        @Qualifier("bubble")
        private SortAlgo sortAlgo;
    
        public int binarySearch(int [] numbers, int target) {
            // Sorting an array
    
            sortAlgo.sort(numbers);
            System.out.println(sortAlgo);
            // Quick sort
    
            // Return the result
            return 3;
        }
    
    }

    We want to do the test with Java Context:

    1. Bring in some dependencies: in pom.xml

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency> -->

    Create a new test file:

    package com.example.in28minutes.basic;
    
    import com.example.in28minutes.In28minutesBasicApplication;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import static org.junit.Assert.assertEquals;
    
    // Load the context
    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = In28minutesBasicApplication.class)
    public class BinarySearchTest {
    
        // Get this bean from context
        @Autowired
        BinarySearchImpl binarySearchImpl;
    
        @Test
        public void testBasicScenario () {
            // call method on binarySearch
            int result = binarySearchImpl.binarySearch(new int[] {}, 5);
    
            // check if the value is correct
            assertEquals(3, result);
        }
    }

    Two things we need to know:

    1. Where can Java find 'BinarySearchImpl', which context it is:

    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = In28minutesBasicApplication.class)

    Basiclly we need to find out, where we register it:

    @SpringBootApplication
    public class In28minutesBasicApplication {
    
        // What are the beans? --@Component
        // What are the dependencies of a bean? -- @AutoWired
        // Where to search for beans => NO NEED
    
        public static void main(String[] args) {
            // Application Context
            ApplicationContext applicationContext =
                    SpringApplication.run(In28minutesBasicApplication.class, args);
            BinarySearchImpl binarySearch = applicationContext.getBean(BinarySearchImpl.class);
           ....
    
        }
    }

    2. Once we load the context and run with spring runner, we can use '@Autowired' to bring in the bean from the context:

    // Get this bean from context
    @Autowired
    BinarySearchImpl binarySearchImpl;
  • 相关阅读:
    搜狗拼音输入法候选框有时会跑到屏幕的左上角
    力扣 94. 二叉树的中序遍历
    让我们了解 Ceph 分布式存储
    HDFS的架构和设计要点
    业级PPTP服务器搭建企
    FastCGI中fastcgi_param 详细说明
    LAMP两种模式
    CentOS 7.4搭建LAMP,LAMP:Linux、Apache、MySQL、PHP
    centos7 7.3php编译安装
    Linux下编译安装MariaDB
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10762549.html
Copyright © 2020-2023  润新知