Spring Boot 使用 Junit

JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。

 

通过观看https://my.oschina.net/sdlvzg/blog/1154281创建项目,再执行以下操作

 

在pom.xml中引入依赖包

		<!-- 测试依赖包 -->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
		</dependency>

 

编写代码:

编写Service

package org.lvgang.service;

import org.springframework.stereotype.Service;

/**
 * 测试类
 * @author Administrator
 */
@Service
public class HelloService {

	public String getName() {
		return "hello";
	}
}

编写Controller

package org.lvgang.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello测试类
 * @author Administrator
 *
 */
@RestController   // 等价于@Controller+@RequstMapping
public class HelloController {
	  @RequestMapping("/hello")  
	  public String hello(){  
	    return "Hello world test!";  
	  }  
}

测试类编写及运行:

测试Service

package org.lvgang.service;

import org.junit.Assert;
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;

/**
 * 测试类
 * @author Administrator
 *
 */
//SpringBoot1.4版本之前用的是SpringJUnit4ClassRunner.class
@RunWith(SpringRunner.class)
//SpringBoot1.4版本之前用的是@SpringApplicationConfiguration(classes = Application.class)
@SpringBootTest
public class HelloServiceTest {

	@Autowired
	private HelloService helloService;

	/**
	 * 测试HelloService类的getName方法
	 */
	@Test
	public void testGetName() {
		Assert.assertEquals("hello", helloService.getName());
	}
}

    在上面类中右击==》Run As ==》1Junit Test ,执行测试类,结果如下:

143713_aMtQ_2273688.png

 

测试Controller(两个方法)

    方法一:

package org.lvgang.controller;


import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

/**
 * 测试类
 * @author Administrator
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class HelloControllerTest {

	@Autowired
    private MockMvc mvc;


    @Test
    public void testHello() throws Exception {
        String uri = "/hello";
        MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON))
            .andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("content:"+content);
        System.out.println("status:"+status);
        assertEquals("错误,正确的返回值为200", status, 200);
		assertThat(content, equalTo("\"Hello world test!\""));
        
    }
}

   

  在上面类中右击==》Run As ==》1Junit Test ,执行测试类,结果如下:

143835_DQGH_2273688.png

    方法二:

package org.lvgang.controller;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.net.URL;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 测试类
 * 
 * @author Administrator
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerTest2 {
    @LocalServerPort
    private int port;

    private URL base;

	@Autowired
	private TestRestTemplate template;
	

    @Before
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/hello");
    }

	@Test
	public void testHello() throws Exception {
		System.out.println("base.toString():"+base.toString());
		ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
		int status = response.getStatusCodeValue();
		String content = response.getBody();
		assertEquals("错误,正确的返回值为200", status, 200);
		assertThat(content, equalTo("Hello world test!"));
	}
}

  在上面类中右击==》Run As ==》1Junit Test ,执行测试类,结果如下:

143900_6g5G_2273688.png

 

转载于:https://my.oschina.net/sdlvzg/blog/1186619

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值