【发布时间】:2018-10-17 01:39:20
【问题描述】:
我目前正在开发一个带有 spring boot 的网站,kotlin,我正在尝试以 xml 格式阅读 Google Trends Rss 提要并将它们解析为 Json。 我想添加单元测试来测试我的控制器,但我不知道到底要测试什么。
这是我的数据类:
data class Rss (
val title: String,
val source: String,
val image: String,
val description: String,
val url: String
)
这是我的休息控制器
@RestController
@RequestMapping(value="/rss")
class RssRestService {
@GetMapping(value = "/list/item")
@CrossOrigin("http://localhost:3000")
fun rss(): List<Rss>? {
val url = "https://trends.google.fr/trends/hottrends/atom/feed?pn=p1"
val reader = XmlReader(URL(url))
val feed: SyndFeed = SyndFeedInput().build(reader)
return feed.entries.subList(1,6)
.map { entry -> Rss(
title = entry.title,
image = entry.foreignMarkup[1].content[0].value.substring(2),
source = entry.foreignMarkup[2].content[0].value,
description = entry.foreignMarkup[3].content[1].value.toString(),
url = entry.foreignMarkup[3].content[1].value
) }
}
到目前为止我所做的测试是
@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class DemoApplicationTests {
@Autowired
lateinit var testRestTemplate: TestRestTemplate
@Test
fun contextLoads() {
}
@Test
fun rssTest() {
val result = testRestTemplate.getForEntity("/rss/list/item", String::class.java)
Assert.assertNotNull(result)
Assert.assertEquals(HttpStatus.OK, result.statusCode)
}
我的问题是:我应该测试什么?以及如何测试输出是否写入?
【问题讨论】:
标签: unit-testing spring-boot junit kotlin spring-restcontroller