2016-03-30 27 views
0

Spring-mvc'de projem var. hata kodu AşağıdaMockito :: MockMVC için MockMVC istisnası için boş gösterici istisnası

Cotroller

@RestController 
@RequestMapping("/api/conferences") 
public class ConferenceController { 

    private static final Logger LOGGER = LoggerFactory.getLogger(ConferenceController.class); 

    @Autowired 
    private ConferenceService conferenceService; 

    @RequestMapping(method = RequestMethod.POST) 
    public Map<String, Object> create(@Valid @RequestBody Conference conference) { 
     LOGGER.debug("Creating a new conference with information: {}", conference); 

     Map<String, Object> response = new LinkedHashMap<>(); 
     response.put("message", "Conference created successfully"); 
     response.put("conference", conferenceService.add(conference)); 
     return response; 
    } 
} 

Service

@Service 
public class ConferenceServiceImpl implements ConferenceService { 

    private static final Logger LOGGER = LoggerFactory.getLogger(ConferenceServiceImpl.class); 

    @Autowired 
    private ConferenceRepository conferenceRepository; 

    @Transactional 
    @Override 
    public Conference add(Conference created) { 
     LOGGER.debug("Creating a new conference with information: {}", created); 

     created = conferenceRepository.save(created); 
     LOGGER.debug("Added a conference with information: {}", created); 

     return created; 
    } 
} 

Test

@Test 
    public void testCreate() throws Exception { 
     Conference added = new Conference.Builder("name", "description").build(); 

     when(conferenceService.add(any(Conference.class))).thenReturn(added); 

     mockMvc.perform(post("/api/conferences") 
       .contentType(TestUtil.APPLICATION_JSON_UTF8) 
       .content(TestUtil.convertObjectToJsonBytes(added)) 
     ) 
       .andExpect(status().isOk()) 
       .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8)) 
       .andExpect(jsonPath("$.message", is("Conference created successfully"))) 
       .andExpect(jsonPath("$.conference.id", is(5))) 
       .andExpect(jsonPath("$.conference.name", is("name"))) 
       .andExpect(jsonPath("$.conference.description", is("description"))); 
    } 

edilir:

ben mockito bir test vakası yazdım, Aşağıda kodudur
org.springframework.dao.InvalidDataAccessApiUsageException: Target object must not be null; nested exception is java.lang.IllegalArgumentException: Target object must not be null 

    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384) 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:227) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436) 
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) 
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
    at com.sun.proxy.$Proxy93.save(Unknown Source) 
    at com.pjwards.aide.service.Conference.ConferenceServiceImpl.add(ConferenceServiceImpl.java:38) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
    at com.sun.proxy.$Proxy96.add(Unknown Source) 
    at com.pjwards.aide.controller.api.ConferenceControllerTest.testCreate(ConferenceControllerTest.java:111) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: java.lang.IllegalArgumentException: Target object must not be null 
    at org.springframework.util.Assert.notNull(Assert.java:115) 
    at org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:205) 
    at org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:138) 
    at org.springframework.beans.AbstractNestablePropertyAccessor.setWrappedInstance(AbstractNestablePropertyAccessor.java:194) 
    at org.springframework.beans.AbstractNestablePropertyAccessor.<init>(AbstractNestablePropertyAccessor.java:133) 
    at org.springframework.beans.BeanWrapperImpl.<init>(BeanWrapperImpl.java:101) 
    at org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.<init>(DirectFieldAccessFallbackBeanWrapper.java:35) 
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.getId(JpaMetamodelEntityInformation.java:144) 
    at org.springframework.data.repository.core.support.AbstractEntityInformation.isNew(AbstractEntityInformation.java:51) 
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.isNew(JpaMetamodelEntityInformation.java:223) 
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:438) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:483) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:468) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
    at com.sun.proxy.$Proxy93.save(Unknown Source) 
    at com.pjwards.aide.service.Conference.ConferenceServiceImpl.add(ConferenceServiceImpl.java:38) 
    at com.pjwards.aide.service.Conference.ConferenceServiceImpl$$EnhancerByMockitoWithCGLIB$$2d3321b.CGLIB$add$0(<generated>) 
    at com.pjwards.aide.service.Conference.ConferenceServiceImpl$$EnhancerByMockitoWithCGLIB$$2d3321b$$FastClassByMockitoWithCGLIB$$38e6884e.invoke(<generated>) 
    at org.mockito.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:216) 
    at org.mockito.internal.creation.cglib.DelegatingMockitoMethodProxy.invokeSuper(DelegatingMockitoMethodProxy.java:19) 
    at org.mockito.internal.invocation.realmethod.DefaultRealMethod.invoke(DefaultRealMethod.java:21) 
    at org.mockito.internal.invocation.realmethod.CleanTraceRealMethod.invoke(CleanTraceRealMethod.java:30) 
    at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:112) 
    at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41) 
    at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93) 
    at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29) 
    at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38) 
    at org.mockito.internal.creation.cglib.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:59) 
    at com.pjwards.aide.service.Conference.ConferenceServiceImpl$$EnhancerByMockitoWithCGLIB$$2d3321b.add(<generated>) 
    ... 47 more 
+0

Gerekli tüm autowired alanları alay mı test Conference nesne kimliği servis

@Service public class ConferenceServiceImpl implements ConferenceService { private static final Logger LOGGER = LoggerFactory.getLogger(ConferenceServiceImpl.class); @Autowired private ConferenceRepository conferenceRepository; @Transactional @Override public Conference add(Conference created) { LOGGER.debug("Creating a new conference with information: {}", created); LOGGER.debug("Added a conference with information: {}", created); return conferenceRepository.save(created); } } 

planı ayrı ve ayrıca ekleyebilir yapmak? – pokemzok

cevap

0

bu değil boş gösterici istisna, sen bazı şey yanlış

Conference added = new Conference.Builder(5, "name", "description").build(); 
İlgili konular