2
votes

I can run my project and run it with no error. But when I run test it can't.
I see the document and step by step.
I don't know where I make mistakes.

config:

@EnableScheduling
@SpringBootApplication
@EnableAsync
@EnableTransactionManagement(proxyTargetClass = true)
@EnableCaching
@MapperScan(basePackages = {"com.cybertron.*.service.dao",
    "com.cybertron.service.auth.dao","com.cybertron.service.mes.api.dao"})
public class CybertronMpsApplication {
  public static void main(String[] args) {
    SpringApplication.run(CybertronMpsApplication.class, args);
  }
}

test file

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@Transactional
@SpringBootTest
@ActiveProfiles("test")
//@ContextConfiguration(classes = TestConfig.class)
public class DeliveryOrderControllerTest {

  @Autowired
  private MockMvc mockMvc;

  @Test
  public void generateInvoiceInfo() throws Exception {
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/order/insert/generateInvoiceInfo")
        .param("deliveryOrderId", "2499");
    mockMvc.perform(requestBuilder);
  }
}

error message:

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.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.runChild(SpringJUnit4ClassRunner.java:94) 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:191) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'asyncApsTask': Unsatisfied dependency expressed through field 'mpsRecordService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'recordService': Unsatisfied dependency expressed through field 'stockService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mdStockService': Unsatisfied dependency expressed through field 'mProductService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MProductServiceImpl': Unsatisfied dependency expressed through field 'warehouseService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mwarehouseService': Unsatisfied dependency expressed through field 'storageLocationService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'storageLocationServiceImpl': Unsatisfied dependency expressed through field 'storageLocationRecordService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'storageLocationRecordServiceImpl': Unsatisfied dependency expressed through field 'outInStorageLockService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'outInStorageLockServiceImpl': Unsatisfied dependency expressed through field 'expectOutInventoryEntityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'expectOutInventoryEntityServiceImpl': Unsatisfied dependency expressed through field 'relationService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DDeliveryOrderAPlanRelationServiceImpl': Unsatisfied dependency expressed through field 'deliveryOrderlineService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryOrderlineServiceImpl': Unsatisfied dependency expressed through field 'deliveryOrderService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryOrderServiceImpl': Unsatisfied dependency expressed through field 'productRecordItemService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productRecordItemService': Unsatisfied dependency expressed through field 'machineWorkCalendarService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MachineWorkCalendarService': Unsatisfied dependency expressed through field 'machineService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ResourceMachineService': Unsatisfied dependency expressed through field 'mProductStatusService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MProductStatusServiceImpl': Unsatisfied dependency expressed through field 'adWfNodeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adWfNodeService': Unsatisfied dependency expressed through field 'workerCenterCapacityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'workerCenterCapacityServiceImpl': Unsatisfied dependency expressed through field 'dataMissingService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataMissingServiceImpl': Unsatisfied dependency expressed through field 'yuseiPurchaseOrderEntityService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cybertron.purchase.service.web.YuseiPurchaseOrderEntityService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ... 24 more Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'recordService': Unsatisfied dependency expressed through field 'stockService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mdStockService': Unsatisfied dependency expressed through field 'mProductService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MProductServiceImpl': Unsatisfied dependency expressed through field 'warehouseService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mwarehouseService': Unsatisfied dependency expressed through field 'storageLocationService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'storageLocationServiceImpl': Unsatisfied dependency expressed through field 'storageLocationRecordService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'storageLocationRecordServiceImpl': Unsatisfied dependency expressed through field 'outInStorageLockService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'outInStorageLockServiceImpl': Unsatisfied dependency expressed through field 'expectOutInventoryEntityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'expectOutInventoryEntityServiceImpl': Unsatisfied dependency expressed through field 'relationService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DDeliveryOrderAPlanRelationServiceImpl': Unsatisfied dependency expressed through field 'deliveryOrderlineService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryOrderlineServiceImpl': Unsatisfied dependency expressed through field 'deliveryOrderService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryOrderServiceImpl': Unsatisfied dependency expressed through field 'productRecordItemService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productRecordItemService': Unsatisfied dependency expressed through field 'machineWorkCalendarService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MachineWorkCalendarService': Unsatisfied dependency expressed through field 'machineService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ResourceMachineService': Unsatisfied dependency expressed through field 'mProductStatusService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MProductStatusServiceImpl': Unsatisfied dependency expressed through field 'adWfNodeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adWfNodeService': Unsatisfied dependency expressed through field 'workerCenterCapacityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'workerCenterCapacityServiceImpl': Unsatisfied dependency expressed through field 'dataMissingService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataMissingServiceImpl': Unsatisfied dependency expressed through field 'yuseiPurchaseOrderEntityService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cybertron.purchase.service.web.YuseiPurchaseOrderEntityService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ... 42 more Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mdStockService': Unsatisfied dependency expressed through field 'mProductService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MProductServiceImpl': Unsatisfied dependency expressed through field 'warehouseService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mwarehouseService': Unsatisfied dependency expressed through field 'storageLocationService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'storageLocationServiceImpl': Unsatisfied dependency expressed through field 'storageLocationRecordService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'storageLocationRecordServiceImpl': Unsatisfied dependency expressed through field 'outInStorageLockService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'outInStorageLockServiceImpl': Unsatisfied dependency expressed through field 'expectOutInventoryEntityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'expectOutInventoryEntityServiceImpl': Unsatisfied dependency expressed through field 'relationService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DDeliveryOrderAPlanRelationServiceImpl': Unsatisfied dependency expressed through field 'deliveryOrderlineService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryOrderlineServiceImpl': Unsatisfied dependency expressed through field 'deliveryOrderService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryOrderServiceImpl': Unsatisfied dependency expressed through field 'productRecordItemService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productRecordItemService': Unsatisfied dependency expressed through field 'machineWorkCalendarService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MachineWorkCalendarService': Unsatisfied dependency expressed through field 'machineService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ResourceMachineService': Unsatisfied dependency expressed through field 'mProductStatusService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MProductStatusServiceImpl': Unsatisfied dependency expressed through field 'adWfNodeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adWfNodeService': Unsatisfied dependency expressed through field 'workerCenterCapacityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'workerCenterCapacityServiceImpl': Unsatisfied dependency expressed through field 'dataMissingService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataMissingServiceImpl': Unsatisfied dependency expressed through field 'yuseiPurchaseOrderEntityService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cybertron.purchase.service.web.YuseiPurchaseOrderEntityService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ... 55 more

3

3 Answers

1
votes

To run your DeliveryOrderControllerTestclass you need to add one more below annotation to DeliveryOrderControllerTest class @SpringBootTest(classes=CybertronMpsApplication .class)

1
votes

I solve it but I dont understand why it can run. @SpringBootTest(classes = DeliveryOrderControllerTest.class)

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(classes = DeliveryOrderControllerTest.class)
@ActiveProfiles("test")
//@ContextConfiguration(classes = TestConfig.class)
public class DeliveryOrderControllerTest {

  @Autowired
  private MockMvc mockMvc;

  @Test
  public void generateInvoiceInfo() throws Exception {
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/order/insert/generateInvoiceInfo")
        .param("deliveryOrderId", "2499");
    mockMvc.perform(requestBuilder);
  }
}
0
votes

As per the Documentation, it worked because

 * Annotation that can be specified on a test class that runs Spring Boot based tests.
 * Provides the following features over and above the regular <em>Spring TestContext
 * Framework</em>:
 * <ul>
 * <li>Uses {@link SpringBootContextLoader} as the default {@link ContextLoader} when no
 * specific {@link ContextConfiguration#loader() @ContextConfiguration(loader=...)} is
 * defined.</li>

However you should do only the following to write tests for controller

@RunWith(SpringRunner.class)
public class DeliveryOrderControllerTest {

    @Mock
    private SomeService service;

    private MockMvc mockMvc;


    @Before
    public void before() throws IOException
    {
        controller = new DeliveryOrderController(settingsService);
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }


   @Test
   public void generateInvoiceInfo() throws Exception {
      // write test here
   }
}