我正在尝试为Java控制器编写Moct测试。当我将其作为junit运行时,此方法工作正常,但是在build中运行时,此操作失败。
这是我的考试班。
@WebAppConfiguration
@RunWith(PowerMockRunner.class)
public class myTestClass {
private MockMvc mockMvc;
@Mock
MyActionScript myActionScript; // It is failing here
@Mock
MyBaseClass myBaseClass; // It is failing here
@InjectMocks
MyControllerClass myControllerClass;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(MyControllerClass).build();
}
@Test
@PrepareForTest({TestClass.class})
public void testmyMethod()throws Throwable{
//SOme code
}
}
这里的问题是,当我尝试模拟我的动作脚本类或myBaseClass时,它就失败了。
这是我的控制器类
@Controller
public class ReportsController extends BaseController {
@RequestMapping(value = "/someurl", method = RequestMethod.POST, consumes = "application/json")
public @ResponseBody somePojo methodList(@RequestBody somePojo requestObj) throws Exception {
somePojo response = (somePojo) this.execute(requestObj,SOME_CONSTANT);
return response;
}
}
我的控制器类扩展到某些抽象类。
这是我的myBaseClass
@Controller
public abstract class myBaseClass extends myActionScript {
//Some Code and some methods
}
如果您还有其他需要,请告诉我。
答案 0 :(得分:0)
尝试使用@RunWith(MockitoJUnitRunner.class)
和@InjectMocks
和@Mock
批注。
您在MyControllerClass中使用MyActionScript和MyBaseClass吗? 如果是,请尝试以下操作:
@WebAppConfiguration
@RunWith(MockitoJUnitRunner.class)
public class myTestClass {
private MockMvc mockMvc;
@Mock
MyActionScript myActionScript; // It is failing here
@Mock
MyBaseClass myBaseClass; // It is failing here
@InjectMocks
MyControllerClass myControllerClass;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(MyControllerClass).build();
}
@Test
@PrepareForTest({TestClass.class})
public void testmyMethod()throws Throwable{
//SOme code
}
}