-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
指定执行多个单元测试类 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
junit-demo/src/main/java/org/wxd/junit/demo/web/SimpleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
junit-demo/src/test/java/org/wxd/junit/demo/simple/SimpleControllerDemoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
junit-demo/src/test/java/org/wxd/junit/demo/simple/SimpleDemoAllTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |