最近,我被分配使用 Sonar 和 Maven 检查 Java 代码覆盖率。我运行mvn sonar:sonar
并且构建出来的很好,而且我可以从仪表板看到结果。
但是,测试用例会以错误的形式出现,如果我正常运行它就不会发生。这是错误堆栈跟踪:
$Proxy11 cannot be cast to com.epsilon.agility.message.dao.MessageDAO
java.lang.ClassCastException: $Proxy11 cannot be cast to com.epsilon.agility.message.dao.MessageDAO
at com.epsilon.agility.message.utils.MessageDBAdaptorService.getMessageDAO(MessageDBAdaptorSer vice.java:32)
at com.epsilon.agility.message.facade.MessageClassificationFacadeImpl.initilaizeDAOs(MessageClassificationFacadeImpl.java:91)
at com.epsilon.agility.message.facade.MessageClassificationFacadeImpl.<init>(MessageClassificationFacadeImpl.java:86)
at com.epsilon.agility.message.dao.test.TestMessageClassificationFacadeImpl.setUp(TestMessageClassificationFacadeImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
以下是错误中涉及的方法:
@BeforeClass
@Transactional(propagation = Propagation.REQUIRED, timeout = 10)
public static void setUp() throws IOException {
conn = TestDBUtils.getDBConnection();
MessageDBAdaptorService.init(conn);
msgclassificationFacade = new MessageClassificationFacadeImpl(conn);
messageDAO = MessageDBAdaptorService.getMessageDAO();
messageAssetDAO = MessageDBAdaptorService.getMessageAssetDAO();
channelDAO = MessageDBAdaptorService.getChannelDAO();
folderItemDAO = MessageDBAdaptorService.getFolderItemDAO();
folderDAO = MessageDBAdaptorService.getFolderDAO();
Assert.assertNotNull(messageDAO);
Assert.assertNotNull(messageAssetDAO);
Assert.assertNotNull(channelDAO);
Assert.assertNotNull(folderItemDAO);
Assert.assertNotNull(folderDAO);
}
我已经在网上搜索了解决方案,但似乎 Sonar 没有类似的解决方案。我怀疑问题可能来自 Sonar 对测试用例的不同方法,因为测试用例在正常情况下运行良好。
如果以上内容还不够,我将发布更多代码和信息。谢谢你的帮助。