2018年4月28日 星期六

Junit 5 整合 Spring 4


<properties>
    <junit.platform.version>1.1.1</junit.platform.version>
    <junit.version>5.1.1</junit.version>
    <spring.version>4.3.16.RELEASE</spring.version>
</properties>
    
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
    
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-engine</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    
    
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>com.github.sbrannen</groupId>
        <artifactId>spring-test-junit5</artifactId>
        <version>1.0.3</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

※好心人提供的 Junit5-Spring4 的 jar 檔,這樣子才有 SpringExtension 可以用

※如果要用 spring-web ,必需多個 spring-web 時;如果要用 spring mvc,必需多個 spring-webmvc




import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import lt.SpringMaven.bean.Animal;
import lt.SpringMaven.bean.Bird;
import lt.SpringMaven.bean.Chicken;
    
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = Animal.class)
public class AppTest {
    @Autowired(required = false)
    private Animal animal;
    
    @Autowired(required = false)
    private Bird bird;
    
    @Autowired(required = false)
    private Chicken chicken;
    
    @Test
    public void testBruce() {
        System.out.println("animal=" + animal);
        System.out.println("bird=" + bird);
        System.out.println("chicken=" + chicken);
    }
}

※spring5才有 @SpringJUnitConfig,所以使用 2.5 的 @ContextConfiguration

沒有留言:

張貼留言