JUnit test fails with java.lang.ClassNotFoundException: com.mysql.jdbc.Driver on Tycho environment

ⅰ亾dé卋堺 提交于 2019-12-13 21:18:13

问题


I have a very strange situation. I have a set of eclipse plugin projects which I am using tycho and maven for building them. I used JDBC driver in one of the projects and I have a test plugin to test this project. Since the com.mysql.jdbc plugin was not available in the eclipse p2 repository ( and we don't have our own p2), I imported the jdbc plugin and created an OSGi plugin and add the dependency to my local plugin.

I have multiple eclipse workspaces sat up. Only in the very first workspace that created the test and jdbc plugin, junit tests are working when I run them using eclipse run as -> Junit test command. When others or even me checkout the source codes and try to run the test in different workspaces, this exception is thrown:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I also tried using tycho integration test (runing mvn integration-test) but that doesn't work neither and I still get the same exception. I tried searching for this a lot but I couldn't find the answer.

UPDATE:I think it is not a tycho problem. I just tried to create the eclipse plug in based on com.jdbc.mysql jar. The workspace I created this on is working fine. But as soon as I commit the code and import the project in another workspace the tests stop working. All of the settings are same among two workspaces, one is working the other gets the ClassNotFound exception!


回答1:


Tycho computes the OSGi runtime based on the transitive dependencies of your test bundle. You probably have no design-time dependency on the mysql driver bundle (but rather only on the JDBC interfaces it implements)

Try adding a test runtime dependency on the mysql jdbc driver bundle. See http://wiki.eclipse.org/Tycho/FAQ#How_to_add_a_undeclared_dependency.3F__.28e.g..2C_OSGi_declarative_service.29 on how to do this.




回答2:


ClassNotFoundExceptions in an OSGi runtime indicate that there is something wrong in the imports and/or export declarations in the OSGi manifests. The most frequent case is that a bundle claims to export a certain package, but doesn't actually have the binaries/class files of that package.

In a Tycho build, this can easily happen if you don't have the entry . in the bin.includes property in the build.properties.



来源:https://stackoverflow.com/questions/16158355/junit-test-fails-with-java-lang-classnotfoundexception-com-mysql-jdbc-driver-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!