使用注解的方式,实现对虚拟数据表 news(id(int),title(varcha(100)),author(varchar(50)),content(text),pubdate(date)) 进行操作的三层结构,写出测试程序
时间: 2023-11-12 17:09:15 浏览: 77
以下是使用注解的方式实现对虚拟数据表news进行操作的三层结构的代码:
1. 实体类News.java
```
@Table(name = "news")
public class News {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
private String title;
private String author;
private String content;
private Date pubdate;
// getter and setter methods
}
```
2. Dao层接口NewsDao.java
```
public interface NewsDao extends JpaRepository<News, Integer> {
}
```
3. Service层实现NewsServiceImpl.java
```
@Service
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsDao newsDao;
@Override
public List<News> findAll() {
return newsDao.findAll();
}
@Override
public News findById(int id) {
return newsDao.findById(id).orElse(null);
}
@Override
public News save(News news) {
return newsDao.save(news);
}
@Override
public void delete(int id) {
newsDao.deleteById(id);
}
}
```
4. 控制层NewsController.java
```
@RestController
@RequestMapping("/news")
public class NewsController {
@Autowired
private NewsService newsService;
@GetMapping("/")
public List<News> findAll() {
return newsService.findAll();
}
@GetMapping("/{id}")
public News findById(@PathVariable int id) {
return newsService.findById(id);
}
@PostMapping("/")
public News save(@RequestBody News news) {
return newsService.save(news);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable int id) {
newsService.delete(id);
}
}
```
5. 测试类NewsControllerTest.java
```
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class NewsControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private NewsDao newsDao;
@Test
public void testFindAll() throws Exception {
mockMvc.perform(get("/news/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(0)));
News news = new News();
news.setTitle("test title");
news.setAuthor("test author");
news.setContent("test content");
news.setPubdate(new Date());
newsDao.save(news);
mockMvc.perform(get("/news/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].title", is("test title")));
}
@Test
public void testFindById() throws Exception {
News news = new News();
news.setTitle("test title");
news.setAuthor("test author");
news.setContent("test content");
news.setPubdate(new Date());
newsDao.save(news);
mockMvc.perform(get("/news/" + news.getId()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title", is("test title")));
}
@Test
public void testSave() throws Exception {
News news = new News();
news.setTitle("test title");
news.setAuthor("test author");
news.setContent("test content");
news.setPubdate(new Date());
mockMvc.perform(post("/news/")
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(news)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title", is("test title")));
mockMvc.perform(get("/news/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].title", is("test title")));
}
@Test
public void testDelete() throws Exception {
News news = new News();
news.setTitle("test title");
news.setAuthor("test author");
news.setContent("test content");
news.setPubdate(new Date());
newsDao.save(news);
mockMvc.perform(delete("/news/" + news.getId()))
.andExpect(status().isOk());
mockMvc.perform(get("/news/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(0)));
}
}
```
阅读全文