我有OuterTest
代码:
private MockMvc mockMvc;
@Mock
private Service Service;
@InjectMocks
private RestController RestController;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = standaloneSetup(RestController)
.build();
}
我有一个嵌套类,有测试,在我的项目上重复了几次,但需要在我的所有控制器上单独测试
@Nested
public class RepeatableTest extends repeatableTestSuite {
}
}
这是测试用例,重复了整个项目
@Test
void repeatableTestCase_shouldFailTest(MockMvc mockMvc, String url, Service service) throws Exception {
mockMvc.perform(post(url, INVALID_VALUE))
.andExpect(status().isBadRequest())
verifyZeroInteractions(service);
}
此测试失败,因为它需要在外部类创建和配置的MockMvc和URL,我该怎么做?
答案 0 :(得分:0)
解决方案是在protected MockMvc mockMvc
中定义repeatableTestSuite
然后在@Nested
类
@Nested
public class RepeatableTest extends RepeatableTestSuite {
@BeforeEach
void setUp() {
mockMvc = OuterClassTest.this.mockMvc;
url = OuterClassTest.URL;
service = OuterClassTest.this.service;
}
}