当从控制器类调用服务类方法时,SpringMVC-Mockito-Junit-NullPointerException
我正在使用SpringMVC编写一个web应用程序。我已经创建了一个服务类来使用UI将东西映射到数据库和从数据库映射出来 我的问题是我对junit和mockito是新手 当我试图在控制器类上编写单元测试时,它总是返回NullPointerException。这真的很烦人,因为我找不到答案,所以我希望你们能帮助我。下面我已经发布了我的代码 更新/返回成功时我的控制器类方法当从控制器类调用服务类方法时,SpringMVC-Mockito-Junit-NullPointerException,spring,mongodb,spring-mvc,junit,mockito,Spring,Mongodb,Spring Mvc,Junit,Mockito,我正在使用SpringMVC编写一个web应用程序。我已经创建了一个服务类来使用UI将东西映射到数据库和从数据库映射出来 我的问题是我对junit和mockito是新手 当我试图在控制器类上编写单元测试时,它总是返回NullPointerException。这真的很烦人,因为我找不到答案,所以我希望你们能帮助我。下面我已经发布了我的代码 更新/返回成功时我的控制器类方法 @RequestMapping(value = "/update", method = RequestMethod.POST)
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateFormData(@ModelAttribute CRDetails certGuide,
ModelMap model) {
crService.updateCertification(certGuide);
return "success";
}
我的服务类方法使用修改后的数据更新mongodb
public void updateCertification(CRDetails certGuide) { String userTypeSubmitted = certGuide.getCertGuidelines().getUserType(); System.out.println("updated by " + userTypeSubmitted); Update update = new Update(); Query query = new Query(Criteria.where("cr").is("CR1"));
CRDetails details = certMongoTemplate.findOne(query, CRDetails.class,
COLLECTION_NAME); CertificateGuidelines newCertGuide = new CertificateGuidelines();
if (details != null) { newCertGuide.setCertG(certGuide.getCertGuidelines().getCertG()); if (userTypeSubmitted.equals("Certification Team")
&& userTypeSubmitted != "") {
newCertGuide.setStatus("Pending with Document Developer"); } else {
newCertGuide.setStatus("Pending with Certification Team"); } newCertGuide.setHistoryCount(++count);
// converting it into mongo document. MongoConverter converter = certMongoTemplate.getConverter(); DBObject newRec = (DBObject) converter
.convertToMongoType(newCertGuide); update.set("certGuidelines", newRec); }
System.out.println("new cert guide " + newCertGuide.getCertG() + " "
+ newCertGuide.getStatus() + " "
+ newCertGuide.getHistoryCount() + " "
+ newCertGuide.getUserType());
certMongoTemplate.updateFirst(query, update, CRDetails.class); }
返回NullPointerException
的我的controllerTest类
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.web.server.MockMvc;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.View;
import com.cerner.docworks.controller.CRController;
import com.cerner.docworks.domain.CRDetails;
import com.cerner.docworks.service.CRService;
public class CRControllerTest {
/*@Mock private CRService service;*/
@Mock View mockView;
@InjectMocks
private CRController crController;
private MockMvc mockMvc;
@Before public void setup() {
}
@Test public void testUpdate() throws Exception { // CRService service = Mockito.mock(CRService.class); CRService crService = Mockito.spy(new CRService()); ModelMap model = Mockito.mock(ModelMap.class); CRDetails certGuide = Mockito.spy(new CRDetails());
CRController controller = new CRController();
// Mockito.when(service.updateCertification(certGuide)); // Mockito.when(model.addAttribute(Matchers.eq("certGuide"),any(String.class))).thenReturn(null); // Mockito.doThrow(new RuntimeException()).when(crService).updateCertification(certGuide); Mockito.doNothing().when(crService).updateCertification(certGuide); Mockito.mockingDetails(certGuide); // Mockito.doCallRealMethod().when(crService).updateCertification(certGuide); // Mockito.(crService)).updateCertification(certGuide);; String fileName = controller.updateFormData(certGuide, model);
System.out.println(fileName); // Mockito.verify(controller).updateFormData(certGuide, model); assertEquals("success", fileName); } }
跟踪:
位于的java.lang.NullPointerException
com.cerner.docworks.controller.CRController.updateFormData(CRController.java:81)
在
com.cerner.docworks.controller.test.CRControllerTest.testUpdate(CRControllerTest.java:62)
位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
invoke(NativeMethodAccessorImpl.java:62)
在
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
位于java.lang.reflect.Method.invoke(Method.java:483)
org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50)
在
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
在
org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47)
在
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
在
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
在
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)位于
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)位于
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)位于
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)位于
org.junit.runners.ParentRunner.run(ParentRunner.java:363)位于
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
在
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
仅供参考,
CRDetails是一个具有
private String id;
private String cr;
private String desc;
private CertificateGuidelines certGuidelines;
private Date dueDate;
//private String userType;
private String solutionName;
仅供参考,
在调试代码时,我发现mock正在将对象设置为null
若我手动设置细节,测试用例通过,但我想使用mockito
提前感谢您的帮助。您需要使用运行测试,例如: 这允许
@injectmock
和@Mock
正常工作。即,mockView
将被创建为视图的模拟,并且crController
将被创建为带有相关@mock
-注释字段的模拟
您可以参考以了解模拟是如何注入的。更好的方法是在控制器上使用构造函数注入。您可以轻松地使用Mockito mock,而不需要反射黑客。
@RunWith(MockitoJUnitRunner.class)
public class CRControllerTest {
// ...