zoukankan      html  css  js  c++  java
  • [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;
  • 相关阅读:
    C#将datatable生成easyui的绑定tree 的json数据格式
    asp.net DataTable转JSON
    asp.net面试题
    windows笔记线程的一些性质
    windows笔记创建线程的另一个函数_beginthreadex
    windows笔记【内核对象线程同步】等待定时器内核对象
    windows笔记用哪个创建线程CreateThread还是_beginthread
    windows笔记【内核对象线程同步】信标内核对象
    windows笔记【内核对象线程同步】事件内核对象
    windows笔记【内核对象线程同步】等待函数
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10762549.html
Copyright © 2011-2022 走看看