복붙노트

[SPRING] Java 프로젝트 : ApplicationContext를로드하지 못했습니다.

SPRING

Java 프로젝트 : ApplicationContext를로드하지 못했습니다.

나는 간단한 JUNIT 테스트 케이스를 작성하고있는 자바 프로젝트를 가지고있다. applicatinoContext.xml 파일을 루트 Java 소스 디렉토리에 복사했습니다. StackOverflow에서 읽은 권장 설정 중 일부를 시도했지만 여전히 동일한 오류가 발생합니다. 이 오류는 내 프로젝트가 자바 프로젝트가 아니라 웹 프로젝트가 아니기 때문에 발생합니까, 아니면 심지어 중요합니까? 나는 어디서 잘못되고 있는지 모르겠습니다.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"C:/projs/sortation/src/main/java/applicationContext.xml"})
// Also tried these settings but they also didnt work,
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})
//@ContextConfiguration("classpath:applicationContext.xml")
@Transactional
public class TestSS {

    @Autowired
    private EmsDao dao;

    @Test
    public void getSites() {

        List<String> batchid = dao.getList();

        for (String s : batchid) {
            System.out.println(s);
        }
    }
}

해결법

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

    1.Maven (src / main / java)을 사용하고있는 것처럼 보입니다. 이 경우 applicationContext.xml 파일을 src / main / resources 디렉토리에 두십시오. 그것은 classpath 디렉토리에 복사 될 것이며 당신은

    Maven (src / main / java)을 사용하고있는 것처럼 보입니다. 이 경우 applicationContext.xml 파일을 src / main / resources 디렉토리에 두십시오. 그것은 classpath 디렉토리에 복사 될 것이며 당신은

    @ContextConfiguration("/applicationContext.xml")
    

    스프링 문서화 : 일반 경로 (예 : "context.xml")는 테스트 클래스가 정의 된 동일한 패키지에서 클래스 경로 리소스로 처리됩니다. 슬래시로 시작하는 경로는 완전한 클래스 경로 위치로 취급됩니다 (예 : "/org/example/config.xml").

    따라서 클래스 경로의 루트 디렉토리에서 파일을 참조 할 때 슬래시를 추가하는 것이 중요합니다.

    절대 파일 경로로 작업하는 경우 'file : C : ...'을 사용해야합니다 (문서가 올바르게 이해 된 경우).

  2. ==============================

    2.나는 같은 문제가 있었고 테스트를 위해 다음 플러그인을 사용하고있었습니다.

    나는 같은 문제가 있었고 테스트를 위해 다음 플러그인을 사용하고있었습니다.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
            <useFile>true</useFile>
            <includes>
                <include>**/*Tests.java</include>
                <include>**/*Test.java</include>
            </includes>
            <excludes>
                <exclude>**/Abstract*.java</exclude>
            </excludes>
            <junitArtifactName>junit:junit</junitArtifactName>
            <parallel>methods</parallel>
            <threadCount>10</threadCount>
        </configuration>
    </plugin>
    

    IDE (eclipse sts)에서 테스트가 제대로 실행되었지만 mvn test 명령을 사용할 때 테스트에 실패했습니다.

    많은 시행 착오 끝에, 위의 플러그인 구성에서 다음 두 줄의 병렬 테스트를 제거하는 것이 해결책이라고 생각했습니다.

        <parallel>methods</parallel>
        <threadCount>10</threadCount>
    

    이 사람이 도움이되기를 바랍니다!

  3. ==============================

    3.내 봄 프로젝트를 부트 스트랩하는 동안이 문제에 직면했습니다. ApplicationListener 를 구현하는 클래스를 사용하고 onApplicationEvent 함수 내에서 예외를 throw합니다.

    내 봄 프로젝트를 부트 스트랩하는 동안이 문제에 직면했습니다. ApplicationListener 를 구현하는 클래스를 사용하고 onApplicationEvent 함수 내에서 예외를 throw합니다.

    내 경우에는 필자는 테스트를 위해 maven surefire 플러그인을 사용 했으므로 테스트 프로세스를 디버깅하려면이 명령을 사용하십시오.

    mvn -Dmaven.surefire.debug test
    
  4. from https://stackoverflow.com/questions/5058294/java-project-failed-to-load-applicationcontext by cc-by-sa and MIT license