我正在使用springMVC编写一个web应用程序。我制作了一个服务类,用于使用UI将事物映射到数据库和从数据库映射事物。
我的问题是我是junit和mockito的新手当我试图在Controller类上编写单元测试时,它总是返回一个NullPointerException。这真的很烦人,因为我找不到答案,所以我希望你们能帮我。下面我发布了我的代码。
我的控制器类方法打开/更新返回成功
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateFormData(@ModelAttribute CRDetails certGuide,
ModelMap model) {
crService.updateCertification(certGuide);
return "success";
}
My Service类方法使用修改后的数据更新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
的类。
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.NullPointerExceptioncom.cerner.docworks.controller.CRController.updateFormData(CRController.java:81)在com.cerner.docworks.controller.test.CRControllerTest.testUpdate(CRControlollerTest.java:62)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:483)org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)在org.junit.runners.model.FrameworkMethod.invokeExploly(FrameworkMethod.java:47)在org.junit.internal.runners.statements.InvokeMethod.eevaluate(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:288)org.junit.runners.ParentRunner.access$000(ParentRunner:58),网址:org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:268)org.junit.runners.ParentRunner.run(ParentRunner:363)org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TtestReference.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)
FYI,CRDetails是一个带有的pojo类
private String id;
private String cr;
private String desc;
private CertificateGuidelines certGuidelines;
private Date dueDate;
//private String userType;
private String solutionName;
FYI,当我调试代码时,我发现mock正在将对象设置为null如果我手动设置细节,测试用例通过,但是,我想使用mockito。
提前感谢您的帮助。
您需要使用MockitoJUnitRunner
运行测试,例如:
@RunWith(MockitoJUnitRunner.class)
public class CRControllerTest {
// ...
这允许@InjectMocks
和@Mock
正常工作。也就是说,mockView
将被创建为View
的模型,而crController
将与相关的@Mock
注释字段一起创建。
您可以参考文档来了解mock是如何注入的。