복붙노트

[SPRING] Spring Autowire로 JUnit 테스트를 작성하는 방법은 무엇입니까?

SPRING

Spring Autowire로 JUnit 테스트를 작성하는 방법은 무엇입니까?

내가 사용하는 파일은 다음과 같습니다.

component.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd 
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <context:component-scan
        base-package="controllers,services,dao,org.springframework.jndi" />
</beans>

ServiceImpl.java

@org.springframework.stereotype.Service
public class ServiceImpl implements MyService {

    @Autowired
    private MyDAO myDAO;

    public void getData() {...}    
}

ServiceImplTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:conf/components.xml")
public class ServiceImplTest{

    @Test
    public void testMyFunction() {...}
}

오류:

16:22:48.753 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2092dcdb] to prepare test instance [services.ServiceImplTest@9e1be92]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services.ServiceImplTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.ServiceImpl services.ServiceImplTest.publishedServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [services.ServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ~[spring-beans.jar:3.1.2.RELEASE]

해결법

  1. ==============================

    1.올바른 패키지를 가져 왔는지 확인하십시오. 올바르게 기억한다면 Autowiring을위한 두 개의 다른 패키지가 있습니다. 다음과 같아야합니다 : org.springframework.beans.factory.annotation.Autowired;

    올바른 패키지를 가져 왔는지 확인하십시오. 올바르게 기억한다면 Autowiring을위한 두 개의 다른 패키지가 있습니다. 다음과 같아야합니다 : org.springframework.beans.factory.annotation.Autowired;

    또한 이것은 이상하게 보입니다.

    @ContextConfiguration("classpath*:conf/components.xml")
    

    나를 위해 잘 작동하는 예제는 다음과 같습니다.

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/applicationContext_mock.xml" })
    public class OwnerIntegrationTest {
    
        @Autowired
        OwnerService ownerService;
    
        @Before
        public void setup() {
    
            ownerService.cleanList();
    
        }
    
        @Test
        public void testOwners() {
    
            Owner owner = new Owner("Bengt", "Karlsson", "Ankavägen 3");
            owner = ownerService.createOwner(owner);
            assertEquals("Check firstName : ", "Bengt", owner.getFirstName());
            assertTrue("Check that Id exist: ", owner.getId() > 0);
    
            owner.setLastName("Larsson");
            ownerService.updateOwner(owner);
            owner = ownerService.getOwner(owner.getId());
            assertEquals("Name is changed", "Larsson", owner.getLastName());
    
        }
    
  2. ==============================

    2.내 생각에 당신의 코드베이스 어딘가에 @ 당신이 @ 인터페이스 클래스 (아마도 MyService) autowiring해야 구체적인 클래스 ServiceImpl을 부탁드립니다.

    내 생각에 당신의 코드베이스 어딘가에 @ 당신이 @ 인터페이스 클래스 (아마도 MyService) autowiring해야 구체적인 클래스 ServiceImpl을 부탁드립니다.

  3. from https://stackoverflow.com/questions/21878714/how-to-write-junit-test-with-spring-autowire by cc-by-sa and MIT license