Spring 整合 Junit4 進行單元測試


1. pom.xml 引入JAR依賴:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.10.RELEASE</version>
    <scope>test</scope>
</dependency>

 

2.編寫測試用例:

@RunWith(SpringJUnit4ClassRunner.class)  注解讓測試運行於Spring測試環境。

@ContextConfiguration注解加載的是Spring的配置文件。

@Test 注解放在需要測試執行的方法上。

package com.phpdragon;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by phpdragon on 2017/4/20.
 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/META-INF/spring/dubbo-provider.xml")
public class AtresiaTest {

    @Test public void test0() {
        System.out.println("test0");
    }

    @Test public void test1() {
        class TestObject implements Runnable {
            public void run(){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        long timeTasks = 0;
        AtresiaDemo demo = new AtresiaDemo();
        try {
            timeTasks = demo.timeTasks(4,new TestObject());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("timeTasks :"+ timeTasks);
    }
}

 

3.右鍵執行:

 

 

 

PS:

http://www.cnblogs.com/wangtj-19/p/5821725.html

https://blog.csdn.net/xxj_jing/article/details/71455522

https://www.cnblogs.com/yanduanduan/p/6548041.html

https://www.cnblogs.com/zxf330301/p/5995126.html

https://blog.csdn.net/dyr_1203/article/details/82965552

https://blog.csdn.net/xiaobing_122613/article/details/80313338

https://blog.csdn.net/qq_32786873/article/details/56480880

https://www.jb51.net/article/134052.htm

https://www.cnblogs.com/aotemanzhifu/p/9192442.html

https://www.cnblogs.com/ggr0305/p/6523574.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM