第二章: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

相关文章
相关标签/搜索