Dat*_*atz 7 java unit-testing mocking spring-boot
我有这个 mvc 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
private MockMvc mockMvc;
@Resource
private WebApplicationContext webApplicationContext;
@MockBean
private UserBO userBO;
@Before
public void init() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
// ...
}
// ...
Run Code Online (Sandbox Code Playgroud)
在一个测试用例中,我需要模拟另一个 bean 来覆盖一个特殊的错误处理分支。这可能吗?有什么方法可以使用,@MockBean
与UserBO
? (PermissionBO
不被重新调整,UserBO
但在与UserBO
被测类相同的级别上使用。)
@Test(expects=IllegalStatusException.class)
public void testErrorHandlingBranch() {
// How can I mock the bean *PermissionsBO* only for this test?
// like @MockBean PermissionsBO permissionsBO;
// Is there a method like injectMockBean(PermissionsBO.class)?
mockMvc.perform(...)
}
Run Code Online (Sandbox Code Playgroud)
liv*_*iur -4
您应该thenReturn()
在测试方法中使用仅在特定测试中使用特定模拟值的方式。
例如:
Mockito.when(userBO.example()).thenReturn("Specific test method mock");
归档时间: |
|
查看次数: |
1579 次 |
最近记录: |