将MockMvc用于Junit参数化测试

yshpjwxd  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(160)

我需要用这么多的测试用例来测试API。正因为如此,我欺骗使用Junit参数化测试。
但是我不能运行我的测试,因为MockMvc不能自动运行,并且它是null。
这是我的测试类:

  1. @RunWith(Parameterized.class)
  2. @AutoConfigureMockMvc
  3. @SpringBootTest
  4. public class BadgeControllerIT {
  5. @Resource
  6. private MockMvc mockMvc;
  7. private final MultiValueMap<String, String> params;
  8. private final String expectedBadgeAsSVG;
  9. private final ResultMatcher status;
  10. public BadgeControllerIT(final MultiValueMap<String, String> params,
  11. final String expectedBadgeAsSVG,
  12. final ResultMatcher status) {
  13. this.params = params;
  14. this.expectedBadgeAsSVG = expectedBadgeAsSVG;
  15. this.status = status;
  16. }
  17. @Parameterized.Parameters
  18. public static Collection<Object[]> parameters() {
  19. return Arrays.asList(BadgeControllerTestsInputProvider.TEST_INPUTS);
  20. }
  21. @Test
  22. public void badgeControllerTests() throws Exception {
  23. mockMvc
  24. .perform(
  25. get("/api/badge")
  26. .queryParams(params)
  27. .accept("image/svg+xml")
  28. )
  29. .andExpect(status)
  30. .andExpect(content().string(expectedBadgeAsSVG));
  31. }
  32. }

在这堂课上我写了我的测试用例:

  1. public class BadgeControllerTestsInputProvider {
  2. public final static Object[][] TEST_INPUTS = new Object[][]{
  3. {
  4. new LinkedMultiValueMap<String, String>(),
  5. readBadge("some-badge"),
  6. status().isOk()
  7. }
  8. };
  9. private static String readBadge(String badge) {
  10. try {
  11. final File svgFile = ResourceUtils.getFile("classpath:testdata/" + badge + ".svg");
  12. return FileUtils.readFileToString(svgFile, StandardCharsets.UTF_8);
  13. } catch (IOException e) {
  14. throw new RuntimeException(e);
  15. }
  16. }
  17. }

当我尝试运行测试时,我遇到了这个异常:

  1. java.lang.NullPointerException: Cannot invoke "org.springframework.test.web.servlet.MockMvc.perform(org.springframework.test.web.servlet.RequestBuilder)" because "this.mockMvc" is null

我也试着自己示例化MockMvc,但我得到了异常:

  1. @RunWith(Parameterized.class)
  2. @WebAppConfiguration
  3. @SpringBootTest
  4. public class BadgeControllerIT {
  5. @Resource
  6. private WebApplicationContext webApplicationContext;
  7. private MockMvc mockMvc;
  8. @Before
  9. public void setup() {
  10. this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
  11. }
  12. .
  13. .
  14. .

例外情况:

  1. java.lang.IllegalArgumentException: WebApplicationContext is required
  2. at org.springframework.util.Assert.notNull(Assert.java:201)
  3. at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.<init>(DefaultMockMvcBuilder.java:52)
  4. at org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup(MockMvcBuilders.java:51)
iibxawm4

iibxawm41#

正如M. Deinum在评论中建议的那样,问题在于使用JUnit4而不是JUnit5。
所以我决定发布一个答案与解决这个问题的任何人有同样的问题。
这是我的测试类现在的样子:

  1. @AutoConfigureMockMvc
  2. @SpringBootTest
  3. public class BadgeControllerIT {
  4. @Resource
  5. private MockMvc mockMvc;
  6. @ParameterizedTest
  7. @ArgumentsSource(BadgeControllerTestsArgumentProvider.class)
  8. public void badgeControllerTests(MultiValueMap<String, String> params, String expectedBadgeAsSVG, ResultMatcher status) throws Exception {
  9. mockMvc
  10. .perform(
  11. get("/api/badge")
  12. .queryParams(params)
  13. .accept("image/svg+xml")
  14. )
  15. .andExpect(status)
  16. .andExpect(content().string(expectedBadgeAsSVG));
  17. }
  18. }

以及测试用例提供程序类:

  1. public class BadgeControllerTestsArgumentProvider implements ArgumentsProvider {
  2. @Override
  3. public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
  4. return Stream.of(
  5. Arguments.of(
  6. new LinkedMultiValueMap<String, String>(),
  7. readBadge("1"),
  8. status().isOk()
  9. )
  10. );
  11. }
  12. private static String readBadge(String badge) {
  13. try {
  14. final File svgFile = ResourceUtils.getFile("classpath:testdata/" + badge + ".svg");
  15. return FileUtils.readFileToString(svgFile, StandardCharsets.UTF_8);
  16. } catch (IOException e) {
  17. throw new RuntimeException(e);
  18. }
  19. }
  20. }
展开查看全部

相关问题