Eclipse编译运行没问题,但执行mvn clean install跑单元测试失败的原因解析

时间:2021-12-01 02:21:53

问题描述:mvn clean install编译工程并运行单元测试出现如下错误

Tests run: 3, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 6.343 sec <<< FAILURE!
selectUserInfosTest(tech.fullink.eaglehorn.test.UserInfoTest) Time elapsed: 0.003 sec <<< ERROR!
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): tech.fullink.eaglehorn.mapper.UserInfoMapper.getUserInfos

at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:178)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:38)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:49)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:42)
at com.sun.proxy.$Proxy18.getUserInfos(Unknown Source)
at tech.fullink.eaglehorn.service.impl.UserInfoServiceImpl.getUserInfos(UserInfoServiceImpl.java:32)
at tech.fullink.eaglehorn.test.UserInfoTest.selectUserInfosTest(UserInfoTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

。。。。。

问题的根源出在这个maven项目的 pom文件上

idea在build工程的时候   遇到maven项目   使用的是pom文件里面配置的<build></build>里面的东西

而这里面如果不做特别配置   是maven默认的   编译的时候   只搬运src/main/java里面的java文件到target/classes,其他文件会被忽略

解决办法就是增加配置

<build>

<finalName>eaglehorn-admin</finalName>

<sourceDirectory>src/main/java</sourceDirectory>

<testSourceDirectory>src/test/java</testSourceDirectory>

<outputDirectory>target/classes</outputDirectory>

<testOutputDirectory>target/test-classes</testOutputDirectory>

<resources>

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.properties</include>

<include>**/*.xml</include>

<include>**/*.json</include>

</includes>

<filtering>false</filtering>

</resource>

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.properties</include>

<include>**/*.xml</include>

</includes>

<filtering>false</filtering>

</resource>

</resources>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

</configuration>

</plugin>

</plugins>

</build>

这样配置之后    idea在builde的时候   或者执行   maven  test  的时候   才能把源码文件夹里的xml文件与java文件一起搬到target/classes   里面去

另外  在eclipse里面   即使pom不做这个特别配置   也没问题    原因是eclipse在build工程的时候   不依赖   maven的pom文件    eclipse的编译器   不会忽略源码文件夹里面的xml文件