[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;