Skip to content

Commit

Permalink
junit demo simple done
Browse files Browse the repository at this point in the history
  • Loading branch information
edf91 committed Jan 1, 2018
1 parent 23f76b0 commit dbae0fc
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
37 changes: 37 additions & 0 deletions junit-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[toc]
# Junit
## 常用注解介绍
### @BeforeClass

在所有测试方法前执行一次
### @AfterClass

在所有测试方法后执行一次
### @Before

在每个测试执行前执行一次
### @After

在每个测试执行后执行一次
### @Test

指定被执行测试的方法
- @Test(timeout = 1000)
- 测试方法执行超时时间,超时则失败
- @Test(expected = Exception.class)
- 指定期望得到的异常,如果抛出该异常,则失败

### @Ignore
可以注解在类或者方法上,表示忽略执行测试

### @RunWith
指定使用那种Runner来调用测试代码

#### Suite.class
打包测试

##### @SuiteClasses
指定执行多个单元测试类



4 changes: 4 additions & 0 deletions junit-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.wxd.junit.demo.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.wxd.junit.demo.service.SimpleService;

@RestController
public class SimpleController {
@Autowired
private SimpleService service;

@RequestMapping(value = "/simple/{id}",method = RequestMethod.GET)
public String of(@PathVariable String id){
return service.of(id);
}

@RequestMapping(value = "/simple/{id}/remove",method = RequestMethod.DELETE)
public void delOf(@PathVariable String id){
service.delOf(id);
}

@RequestMapping(value = "/simple/add",method = RequestMethod.POST)
public void add(String id){
service.save(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.wxd.junit.demo.simple;


import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
@FixMethodOrder(value = MethodSorters.JVM)
public class SimpleControllerDemoTest {

MockMvc mvc;
@Autowired
WebApplicationContext context;
static String id = "id";

@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context).build();

mvc.perform(MockMvcRequestBuilders.post("/simple/add").param("id",id))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
public void assertOfId() throws Exception {
String qId = mvc.perform(MockMvcRequestBuilders.get(String.format("/simple/%s",id)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();
Assert.assertEquals(id,qId);
}

@Test
public void assertDel() throws Exception {
remove();
String qId = mvc.perform(MockMvcRequestBuilders.get(String.format("/simple/%s",id)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();

Assert.assertEquals("",qId);
}


private void remove() throws Exception {
mvc.perform(
MockMvcRequestBuilders.delete(String.format("/simple/%s/remove",id))
).andExpect(MockMvcResultMatchers.status().isOk());
}


@After
public void clearUp() throws Exception {
remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.wxd.junit.demo.simple;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({SimpleServiceDemoTest.class, SimpleControllerDemoTest.class})
public class SimpleDemoAllTest {
}

0 comments on commit dbae0fc

Please sign in to comment.