Spring各种环境下使用Junit和Testng框架姿势

Spring各种环境下使用Junit和Testng框架整合

Spring应用程序

使用Junit获取容器对象

在线源码查阅

  • 方式一无继承方式

/**

 *

 *

 * 普通的Spring程序 在使用junit框架时可以以下写法

 * SpringJUnit4ClassRunner.class

 * SpringRunner.class效果相同

 *

 */

//@RunWith(SpringJUnit4ClassRunner.class)

@RunWith(SpringRunner.class)

@ContextConfiguration(classes = ApplicationContexConfig.class)

public class SpringIOCWithJunit {

    @Autowired

    Peson person;

    @Test

    public void TestIoc() {

        System.out.println(person);

    }







}
  • 方式二继承AbstractJUnit4SpringContextTests
@ContextConfiguration(classes = ApplicationContexConfig.class)

public  class SpringIOCWithJunitTWo extends AbstractJUnit4SpringContextTests {



    @Autowired

    Peson person;

    @Test

    public void TestIoc() {

        System.out.println(person);

    }

}

使用TestNG获取容器对象

  • 继承AbstractTestNGSpringContextTests
@ContextConfiguration(classes = ApplicationContexConfig.class)

public class SpringIocWithTestNG extends AbstractTestNGSpringContextTests {



    @Autowired

    Peson person;



    @Test

    public void TestIoc() {

        System.out.println(person);

    }



}

SpringWeb应用程序

  • 需要在Spring应用基础上增加一个注解@WebAppConfiguration的注解

SpringBoot应用程序

使用Junit获取容器对象

  • 方式一无继承方式
package com.SwaggerTest.BaseTest;


import com.SwaggerTest.bean.Parent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Spring 使用JUnit 框架 并使用Ioc注入功能
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class SpringBootIocWithJunit {
    @Autowired
    Parent parent;

    @Test
    public void TestSpringBootIOCWhitJUnit() {
        System.out.println(parent);
    }

}
  • 方式二继承AbstractJUnit4SpringContextTests
package com.SwaggerTest.BaseTest;


import com.SwaggerTest.bean.Parent;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

/**
 * 第二种方式
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringBootIocWithJunitTwo extends AbstractJUnit4SpringContextTests {
    @Autowired
    Parent parent;

    @Test
    public void TestSpringBootIOCWhitJUnit() {
        System.out.println(parent);
    }

}

使用TestNG获取容器对象

  • 继承AbstractTestNGSpringContextTests
package com.SwaggerTest.BaseTest;


import com.SwaggerTest.bean.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

/**
 * Spring Testng 框架 并使用Ioc注入功能
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringBootIocWithTestNG extends AbstractTestNGSpringContextTests {
    @Autowired
    Parent parent;

    @Test
    public void TestSpringBootIOCWhitTestNG() {
        System.out.println(parent);
    }

}

SpringBoot项目代码

你可能感兴趣的:(SpringBoot,Junit,Testng)