@ RunWith(SpringRunner.class)@Runwith(PowerMockRunner.class)
如何合并这两个注释。由于RunWith仅支持单个值。
如果您正在使用JUnit 5,则您不能使用它,因为PowerMock还不是support it。
使用JUnit 4使用@Runwith(PowerMockRunner.class)
,并使用SpringRunner.class
注释添加@PowerMockRunnerDelegate
。例如
import org.springframework.test.context.junit4.SpringRunner;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest(LogHelper.class)
@WebMvcTest(controllers = UserController.class)
public class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@Mock
private UserService userService;
@Test
public void saveShouldReturnOK() {
mockStatic(LogHelper.class);
doNothing().when(LogHelper.info(anyString()));
....
}
}