2013-01-15 4 views
1

환경 존중하지 :
이클립스 주노
Junit와를 4.11
받는다는 3.0.4
JUnit을 받는다는 클래스 경로를

문제 :

나는 일식의에서 단위 테스트를 실행할 때 나는 클래스 로딩 문제가 생각 주니어 러너. 내 특정 문제는이 코드에서 유래 :

this.getClass().forName(type); 

위의 코드는 메이븐 의존성에 살고있는 클래스의 이름으로 결국 전화를받을 것이다 (종속성은 단순히 자바 콩의 모음입니다). mvn 테스트를 실행할 때이 코드는 성공적으로 실행됩니다. 그러나 junit 테스트를 실행하기 위해 Eclipse를 사용하는 경우 클래스를 찾을 수 없습니다. 예외가 발생합니다.

실행 구성의 클래스 경로 탭에있는 classpath 항목은 종속성을 참조합니다. 따라서 종속성이 왜 끌어 들여지는지 잘 모르겠습니다.

왜 이런 일이 벌어 질까요? 나는 maven plugin을 Eclipse에 설치했기 때문에 같은 classpath를 사용해야한다. 그러나 모든 것이 동일하지는 않은 것처럼 보입니다. 나는 청소를하고, maven 프로젝트 설정을 업데이트하고, Eclipse를 다시 시작하려고 시도했다.

.classpath 파일

<?xml version="1.0" encoding="UTF-8"?> 
<classpath> 
    <classpathentry kind="src" output="target/classes" path="src/main/java"> 
     <attributes> 
      <attribute name="optional" value="true"/> 
      <attribute name="maven.pomderived" value="true"/> 
     </attributes> 
    </classpathentry> 
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> 
     <attributes> 
      <attribute name="maven.pomderived" value="true"/> 
     </attributes> 
    </classpathentry> 
    <classpathentry kind="src" output="target/test-classes" path="src/test/java"> 
     <attributes> 
      <attribute name="optional" value="true"/> 
      <attribute name="maven.pomderived" value="true"/> 
     </attributes> 
    </classpathentry> 
    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> 
     <attributes> 
      <attribute name="maven.pomderived" value="true"/> 
     </attributes> 
    </classpathentry> 
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"> 
     <attributes> 
      <attribute name="maven.pomderived" value="true"/> 
     </attributes> 
    </classpathentry> 
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> 
     <attributes> 
      <attribute name="maven.pomderived" value="true"/> 
     </attributes> 
    </classpathentry> 
    <classpathentry kind="output" path="target/classes"/> 
</classpath> 

의 .project 파일

<?xml version="1.0" encoding="UTF-8"?> 
<projectDescription> 
    <name>brokenClasspathproject</name> 
    <comment></comment> 
    <projects> 
    </projects> 
    <buildSpec> 
     <buildCommand> 
      <name>org.eclipse.wst.common.project.facet.core.builder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
     <buildCommand> 
      <name>org.eclipse.jdt.core.javabuilder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
     <buildCommand> 
      <name>org.eclipse.m2e.core.maven2Builder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
    </buildSpec> 
    <natures> 
     <nature>org.eclipse.jdt.core.javanature</nature> 
     <nature>org.eclipse.m2e.core.maven2Nature</nature> 
     <nature>org.eclipse.wst.common.project.facet.core.nature</nature> 
    </natures> 
</projectDescription> 


의 JUnit 설정

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig"> 
<stringAttribute key="bad_container_name" value="/brokenClasspathproject/junit"/> 
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS"> 
<listEntry value="/brokenClasspathproject"/> 
</listAttribute> 
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES"> 
<listEntry value="4"/> 
</listAttribute> 
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value="=brokenClasspathproject"/> 
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/> 
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/> 
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/> 
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/> 
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/> 
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="brokenClasspathproject"/> 
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/> 
</launchConfiguration> 
+0

테스트 클래스 경로는 Maven의 Eclipse 프로젝트 클래스 경로에 포함되어 있지 않습니다. 그렇지 않으면 모든 종류의 지옥이 생길 것입니다. 실행 목표에서 클래스 경로를 수동으로 설정했는지 확인하십시오. –

+0

pom.xml 콘텐츠를 공유 할 수 있습니까? 어쩌면 당신은 단지 테스트를위한 의존성을 가지고 있습니다. –

+0

@EugenioCuevas하지만, 그것이 참조하는 종속성에는 정의 된 범위가 없습니다. – ctwomey1

답변

1

내가 왜 이런 일 알아 낸 것 같아요!

내가 실행 된 코드는 간단했다 : 받는다는에서 일하는

this.getClass().forName(name); 

하지만 JUnit을 독립은. 그러나 내가 내 재산을 변경했을 때 :

this.getClass().forName(com.packagename.name); 

코드가 작동하기 시작했습니다. 나는 이름과 com.packagename.name을 인식 할 수있는 몇 가지 백엔드 부두를 만들고있는 것으로 확신한다. javadocs를 읽으려고 실제로 멈추었다면 class.forName이 패키지 이름을 기대한다는 것을 알았을 것입니다.

그래서 도덕적으로 실제로 javadocs를 읽는 것 같습니다. 내 사과 (그리고 진심으로 감사합니다!) 이것에 대해 논평 한 모든 사람들에게!