저는 Maven에 젖은 채로 젖 혔지 만 가파른 학습 곡선입니다. 단위 테스트 내에서 일반 텍스트 파일을로드하려고합니다. 테스트가 통과되었으므로 이제 프로젝트를 maven 프로젝트로 변환했습니다. 테스트 파일이있는 위치에서 테스트가 해결되지 않는 것 같습니다.Maven에서 내 단위 테스트에서 사용하는 일부 텍스트 파일을 볼 수 없습니다.
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.stevenpeterson</groupId>
<artifactId>bookreader</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>bookreader</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>
</project>
내가 단위 테스트를 실행하면 :
// I have the following test that I would like to run:
package net.stevenpeterson.base;
import static org.junit.Assert.*;
import net.stevenpeterson.base.LoadStringUtility;
import org.junit.Test;
public class LoadStringUtilityTest {
@Test
public void testSingleLine() {
assertEquals("Loading File", "This is a test." , loadFile("singleLine"));
}
@Test
public void testSeveralLines() {
assertFalse("Compare a string appended with extra lines, should not compare true. ", "This is a test.".equals(loadFile("severalLines")));
}
@Test
public void loadSherlockHolmes() {
String fileToLoad = "cano.txt";
try{
StringBuilder holmesCanon = LoadStringUtility.LoadString(fileToLoad);
System.out.println("Finished Loading file: chars read=" + holmesCanon.length());
assertTrue("loading size of file:", true);
}catch(Exception e){
fail("Exception thrown while loading: " + fileToLoad);
}
}
private String loadFile(String fileName) {
StringBuilder loadedFromFile = new StringBuilder();
try {
loadedFromFile = LoadStringUtility.LoadString(fileName);
} catch (Exception e) {
fail("Unable to find load file: " + fileName);
e.printStackTrace();
}
return loadedFromFile.toString();
}
}
다음
내 pom.xml 파일입니다
[email protected]:~/maven-conversion/bookreader$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building bookreader 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ bookreader ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/steven/maven-conversion/bookreader/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ bookreader ---
[INFO] Compiling 3 source files to /home/steven/maven-conversion/bookreader/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ bookreader ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ bookreader ---
[INFO] Compiling 5 source files to /home/steven/maven-conversion/bookreader/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ bookreader ---
[INFO] Surefire report directory: /home/steven/maven-conversion/bookreader/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.stevenpeterson.base.LoadStringUtilityTest
Tests run: 3, Failures: 3, Errors: 0, Skipped: 0, Time elapsed: 0.041 sec <<< FAILURE!
Running net.stevenpeterson.base.SplitStringUtilityTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec
Running net.stevenpeterson.booksplitter.BookSplitterTest
Tests run: 4, Failures: 4, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec <<< FAILURE!
Results :
Failed tests:
testSingleLine(net.stevenpeterson.base.LoadStringUtilityTest): Unable to find load file: singleLine
testSeveralLines(net.stevenpeterson.base.LoadStringUtilityTest): Unable to find load file: severalLines
loadSherlockHolmes(net.stevenpeterson.base.LoadStringUtilityTest): Exception thrown while loading: cano.txt
ThirdLineOfAlphabetTest(net.stevenpeterson.booksplitter.BookSplitterTest): IOException thrown while loading test file.
OutOfRangeLineOfAlphabetTest(net.stevenpeterson.booksplitter.BookSplitterTest): IOException thrown while loading test file.
SectionSizeTwoAlphabetTest(net.stevenpeterson.booksplitter.BookSplitterTest): IOException thrown while loading test file.
LastLineOfAlphabetTest(net.stevenpeterson.booksplitter.BookSplitterTest): IOException thrown while loading test file.
> Tests run: 12, Failures: 7, Errors: 0, Skipped: 0
>
> [INFO] ------------------------------------------------------------------------
> [INFO] BUILD FAILURE
> [INFO] ------------------------------------------------------------------------
> [INFO] Total time: 1.634s
> [INFO] Finished at: Tue Nov 29 19:41:59 MST 2011
> [INFO] Final Memory: 15M/105M
> [INFO] ------------------------------------------------------------------------
> [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.7.2:test
> (default-test) on project bookreader: There are test failures.
> [ERROR]
> [ERROR] Please refer to /home/steven/maven-conversion/bookreader/target/surefire-reports for
> the individual test results.
> [ERROR] -> [Help 1]
> [ERROR]
> [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
> [ERROR] Re-run Maven using the -X switch to enable full debug logging.
> [ERROR]
> [ERROR] For more information about the errors and possible solutions, please read the following articles:
> [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Contents of: /home/steven/maven-conversion/bookreader/target/test-classes
AlphabetTest cano.txt net/ severalLines singleLine
It looks as if Maven is placing the resources in the correct location, I think I'm confused about the relative path that is being used when I execute my tests.
:
그 후, 나는 테스트 클래스를 수정 다른 패키지/폴더. 그래서, 내가이 문제를 해결하기 위해 사용하는 빠르고 더러운 접근법은 각 테스트 파일의 패키지/폴더의 전체 경로를 포함하는 것입니다. –