图结构junit测试NullPointerException

我为映射类进行单元测试,但每次它都会显示我NullPointerException

@Mapper(componentModel = "spring")
public abstract class CountryMapper implements EntityMapper<CountryDTO, Country> {

  static final public CountryMapper INSTANCE = Mappers.getMapper(CountryMapper.class);

  @Autowired
  CountryRepository countryRepository;

  Country fromId(Long id) {
    if (id == null) {
      return null;
    }
    return countryRepository.findOne(id);
  }
}


public class CountryMapperUnitTest {

  CountryMapper countryMapper = CountryMapper.INSTANCE;

  @Test
public void givenIdCountrytoCountry_whenMaps_thenCorrect() {

    Long id = 1L;
    Country country = countryMapper.fromId(id);
    assertEquals(id, country.getId()); //NullPointerException

}

java.lang.NullPointerException at com.soft.prg.service.mapper.CountryMapperUnitTest.givenCountryDTOtoCountry_whenMaps_thenCorrect(CountryMapperUnitTest.java:26)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java :62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod .java:50),位于org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)。 internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)在org.junit.runners.BlockJUnit4ClassRunner.runChild(Block JUnit4ClassRunner.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)at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at org.junit。 runners.ParentRunner.run在org.eclipse.jdt.internal.junit.runner.TestExecution(ParentRunner.java:363)在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)。运行(TestExecution.java:38)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner。的java:678)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

当我在类上添加@RunWith (SpringRunner.class)时,它向我显示了这个错误

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'com.soft.prg.service.mapper.CountryMapperUnitTest'的bean时出错:通过字段'countryMapper'表示的不满意的依赖项; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的bean类型'com.soft.prg.service.mapper.CountryMapper'可用:预计至少有1个bean有资格作为autowire候选者。 依赖注释:位于org.springframework的org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)处的{@ org.springframework.beans.factory.annotation.Autowired(required = true)}。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory的org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)处的beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)。使用org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)在org.springframework.test.context.suppor t.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java: 228)在org.springframework的org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)处的org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)。 test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner。 rungild(SpringJUnit4ClassRunner.java:94)at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)a 牛逼org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)维持在org.junit.runners.ParentRunner $ 2.evaluate org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)(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)在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)在org.eclipse.jdt.internal.junit4.runner。 JUnit4TestReference.run(JUnit4TestReference.java:86)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:459)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)at org.eclip org.springframework:se.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)所致。 beans.factory.NoSuchBeanDefinitionException:不存在可用的'com.soft.prg.service.mapper.CountryMapper'类型的合格bean:期望至少1个符合自动装配候选者的bean。 依赖注释:位于org.springframework.beans的org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493){@ org.springframework.beans.factory.annotation.Autowired(required = true)}。 factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement。注入(AutowiredAnnotationBeanPostProcessor.java:585)... 28更多

链接地址: http://www.djcxy.com/p/90373.html

上一篇: mapstruct junit test NullPointerException

下一篇: Java with mockito fails