forked from liyifeng1994/ssm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request liyifeng1994#25 from meiwen1111/master
add test for BookController
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/test/java/com/soecode/lyf/web/AbstractContextControllerTests.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,15 @@ | ||
package com.soecode.lyf.web; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
@WebAppConfiguration | ||
@ContextConfiguration({ "classpath:spring/spring-web.xml", "classpath:spring/spring-service.xml", | ||
"classpath:spring/spring-dao.xml" }) | ||
public class AbstractContextControllerTests { | ||
|
||
@Autowired | ||
protected WebApplicationContext wac; | ||
} |
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,64 @@ | ||
package com.soecode.lyf.web; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; | ||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.transaction.TransactionConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** | ||
* | ||
* @author Kemin | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
// 配置事务的回滚,对数据库的增删改都会回滚,便于测试用例的循环利用 | ||
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) | ||
@Transactional | ||
public class BookControllerTest extends AbstractContextControllerTests { | ||
|
||
private MockMvc mockMvc; | ||
private String listUrl = "/book/list"; | ||
private String detailUrl = "/book/{bookId}/detail"; | ||
private String appointUrl = "/book/{bookId}/appoint"; | ||
private long bookId = 1000; | ||
|
||
@Before | ||
public void setup() { | ||
this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).alwaysDo(print()).build(); | ||
} | ||
|
||
@Test | ||
public void list() throws Exception { | ||
this.mockMvc.perform(get(listUrl)).andExpect(view().name("list")); | ||
} | ||
|
||
@Test | ||
public void existDetail() throws Exception { | ||
this.mockMvc.perform(get(detailUrl, bookId)).andExpect(view().name("detail")) | ||
.andExpect(model().attributeExists("book")); | ||
} | ||
|
||
@Test | ||
public void notExistDetail() throws Exception { | ||
this.mockMvc.perform(get(detailUrl, 1100)).andExpect(forwardedUrl("/book/list")); | ||
} | ||
|
||
@Test | ||
public void appointTest() throws Exception { | ||
this.mockMvc.perform(post(appointUrl, bookId).param("studentId", "1").accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(content().contentType("application/json;charset=utf-8")); | ||
} | ||
} |