public interface ShowsService {
List<Show> shows();
}
@Service
public class ShowsServiceImpl implements ShowsService {
@Override
public List<Show> shows() {
return List.of(
new Show("Stranger Things", 2016),
new Show("Ozark", 2017),
new Show("The Crown", 2016),
new Show("Dead to Me", 2019),
new Show("Orange is the New Black", 2013)
);
}
}
Kotlin:
interface ShowsService {
fun shows(): List<ShowsDataFetcher.Show>
}
@Service
class BasicShowsService : ShowsService {
override fun shows(): List<ShowsDataFetcher.Show> {
return listOf(
ShowsDataFetcher.Show("Stranger Things", 2016),
ShowsDataFetcher.Show("Ozark", 2017),
ShowsDataFetcher.Show("The Crown", 2016),
ShowsDataFetcher.Show("Dead to Me", 2019),
ShowsDataFetcher.Show("Orange is the New Black", 2013)
)
}
}
@DgsComponent
class ShowsDataFetcher {
@Autowired
lateinit var showsService: ShowsService
@DgsData(parentType = "Query", field = "shows")
fun shows(@InputArgument("titleFilter") titleFilter: String?): List<Show> {
return if (titleFilter != null) {
showsService.shows().filter { it.title.contains(titleFilter) }
} else {
showsService.shows()
}
}
}
目前来说,现在这个例子的数据还是来源于内存,想象一下,这个 Service 真的去调用了一个外部的数据存储。我们一起尝试在测试中 Mock 这个 Service。
Java:
@SpringBootTest(classes = {DgsAutoConfiguration.class, ShowsDataFetcher.class})
public class ShowsDataFetcherTests {
@Autowired
DgsQueryExecutor dgsQueryExecutor;
@MockBean
ShowsService showsService;
@BeforeEach
public void before() {
Mockito.when(showsService.shows()).thenAnswer(invocation -> List.of(new Show("mock title", 2020)));
}
@Test
public void showsWithQueryApi() {
GraphQLQueryRequest graphQLQueryRequest = new GraphQLQueryRequest(
new ShowsGraphQLQuery.Builder().build(),
new ShowsProjectionRoot().title()
);
List<String> titles = dgsQueryExecutor.executeAndExtractJsonPath(graphQLQueryRequest.serialize(), "data.shows[*].title");
assertThat(titles).containsExactly("mock title");
}
}
Kotlin:
@SpringBootTest(classes = [DgsAutoConfiguration::class, ShowsDataFetcher::class])
class ShowsDataFetcherTest {
@Autowired
lateinit var
dgsQueryExecutor:DgsQueryExecutor
@MockBean
lateinit var
showsService:ShowsService
@BeforeEach
fun before() {
Mockito.`when`(showsService.shows()).thenAnswer {
listOf(ShowsDataFetcher.Show("mock title", 2020))
}
}
@Test
fun shows() {
val titles :List<String> =dgsQueryExecutor.executeAndExtractJsonPath("""
{
shows {
title
releaseYear
}
}
""".trimIndent(), "data.shows[*].title")
assertThat(titles).contains("mock title")
}
}
@Test
void showsWithException() {
Mockito.when(showsService.shows()).thenThrow(new RuntimeException("nothing to see here"));
ExecutionResult result = dgsQueryExecutor.execute(" { shows { title releaseYear }}");
assertThat(result.getErrors()).isNotEmpty();
assertThat(result.getErrors().get(0).getMessage()).isEqualTo("java.lang.RuntimeException: nothing to see here");
}
Kotlin:
@Test
fun showsWithException() {
Mockito.`when`(showsService.shows()).thenThrow(RuntimeException("nothing to see here"))
val result = dgsQueryExecutor.execute("""
{
shows {
title
releaseYear
}
}
""".trimIndent())
assertThat(result.errors).isNotEmpty
assertThat(result.errors[0].message).isEqualTo("java.lang.RuntimeException: nothing to see here")
}