java - eclipse junit 问题:"method initializationerror not found"
巴扎黑
巴扎黑 2017-04-17 17:07:36
0
2
613

创建了一个Junit Test Case 代码如下:

package com.example; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver; import java.util.Properties; public class Test { @org.junit.Test public void test() { System.out.println("hello"); } /** * 编写一个通用的方法, 在不修改源程序的情况下, 可以获取任何数据库的连接 * 解决方案: 把数据库驱动 Driver 实现类的全类名、url、user、password 放入一个 * 配置文件中, 通过修改配置文件的方式实现和具体的数据库解耦. * @throws Exception */ @org.junit.Test public Connection getConnection() throws Exception{ String driverClass = null; String jdbcUrl = null; String user = null; String password = null; //读取类路径下的 jdbc.properties 文件 InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(in); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("jdbcUrl"); user = properties.getProperty("user"); password = properties.getProperty("password"); //通过反射常见 Driver 对象. Driver driver = (Driver) Class.forName(driverClass).newInstance(); Properties info = new Properties(); info.put("user", user); info.put("password", password); //通过 Driver 的 connect 方法获取数据库连接. Connection connection = driver.connect(jdbcUrl, info); return connection; } @org.junit.Test public void testGetConnection() throws Exception{ System.out.println(getConnection()); } }

可是在方法上 右键 -- Run As -- JUnit Test 出现如下错误

出现如下提示框:

在网上查找: 说是因为缺少hamcrest的jar包, 导入后依然有上面的错误, 下面是我的工程的目录. junit.jar是 4.12 版本的.

这个问题怎么解决呢?

巴扎黑
巴扎黑

Antworte allen (2)
左手右手慢动作

上图中:在JUnit4里面已经有了junit.jar,hamcrest依赖。
lib文件夹里又有一个hamcrest依赖。

现在是有了2个hamcrest的依赖,怀疑是冲突了,建议删除lib下的hamcrest试试

    伊谢尔伦

    你放的不对啊,Eclipse里给普通的java项目添加jar依赖通过如下步骤:

      Neueste Downloads
      Mehr>
      Web-Effekte
      Quellcode der Website
      Website-Materialien
      Frontend-Vorlage
      Über uns Haftungsausschluss Sitemap
      Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!