第二章:SpringBoot基礎知識-springboot代碼測試

源碼下載:https://u11556602.ctfile.com/fs/11556602-361219278java

                https://download.csdn.net/download/qq_36267875/11089023web

如今的程序裏面已經實現了一個最爲簡單的控制器程序類,不過從實際的項目角度來說,必需要求考慮到代碼的測試問題,並且如今的程序代碼屬於springboot,則須要在你的項目之中進行以下的pom.xml文件的變動spring

1.【microboot-base模塊】修改pom.xml配置文件,追加springboot的測試支持類;apache

<!-- springboot測試類 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

完整pom以下springboot

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.mldn</groupId>
    <artifactId>microboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microboot-base</artifactId>
  <name>microboot-base</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>  
	<!-- springboot測試類 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>	
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

只要進行java測試,最簡單實用的就是junit,因此這個開發包必定要隨測試一塊兒導入,app

2.【microboot-base模塊】創建一個測試程序類;maven

package cn.mldn.microboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {
    @RequestMapping("/")
    @ResponseBody
   public String home() {
        return "www.mldn.cn";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

 

package cn.mldn.microboot.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import cn.mldn.microboot.SampleController;
import junit.framework.TestCase;

@SpringBootTest(classes=SampleController.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestSampleController {
	@Resource
	private SampleController sampleController;
	@Test
	public void testHome(){
		TestCase.assertEquals(this.sampleController.home(), "www.mldn.cn");
	}
}

測試成功!spring-boot

相關文章
相關標籤/搜索