@RunWith(SpringRunner.class) @Runwith(PowerMockRunner.class)
如何合并这两个注释。由于 RunWith 仅支持单个值。
最佳答案
如果您正在使用JUnit 5,则无法使用它,因为PowerMock不支持support it还没有。
对于JUnit 4,使用@Runwith(PowerMockRunner.class)
并使用@PowerMockRunnerDelegate
添加SpringRunner.class
注解。例如
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()));
....
}
}
关于java - 如何在Java JUnit中将这两个RunWith结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59640289/