• [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;
  • 相关阅读:
    转发自lealii 的博客,如有侵权请通知删除
    SQL注入详解-转发
    IT实用技术资源整理
    JMeter-接口自动化之正则表达式关联
    转发自_destiny如有侵权请联系删除
    二元数组的最小值最大和
    连续区间和大于等于某个数(弱化版)
    独一无二的最小生成树
    牛客小白月赛19 弱版 C
    牛客 安徽大学新生赛 G
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10762549.html
Copyright © 2020-2023  润新知