Org.dbunit.dataset.DataSetException:java.net.MalformedURLException Solution of Dbunit

Source: Internet
Author: User

First, problem positioning

When you use Dbunit to do a DAO unit test, a Org.dbunit.dataset.DataSetException:java.net.MalformedURLException exception is thrown, such as


Can see the problem appears in my written testdbunit 49 lines, and then look at the source code


Line 49th is this sentence idataset ds = new Flatxmldataset (file). But what actually happens is the TestDbUnit.class.getClassLoader () in the File object. getResourceAsStream ("T_user.xml"),


I will print out this sentence to find the result:


The result is null, stating that the T_user.xml file was not read, which is why the org.dbunit.dataset.DataSetException:java.net.MalformedURLException was generated.


Second, the solution

The root cause of the problem is that the T_use.xml file is not read, and why this can happen, first look at the location of my t_user.xml file,

The T_use.xml file is under the Dbunit_xml package, and the Testdbunit class that parses it is under the Zttc.itat.service package.


method One:

Now we move the T_use.xml file to the test root and find the problem resolved in the run:

The printed TestDbUnit.class.getClassLoader (). getResourceAsStream ("T_user.xml") is no longer null.


Method Two:

Change TestDbUnit.class.getClassLoader (). getResourceAsStream ("T_user.xml") to TestDbUnit.class.getResourceAsStream ("T_ User.xml ") and then moving the T_user.xml to the same package as testdbunit can also solve the problem:



Method Three:

If I just want to put T_user.xml under the Dbunit_xml package to read what to do, you can use this statement TestDbUnit.class.getResourceAsStream ("/dbunit_xml/t_user.xml"), Run without errors



Third,Class.getresourceasstream (string path) and Class.getClassLoader.getResourceAsStream (string path)

1. Class.getresourceasstream (String Path): path does not start with '/' by default, the resource is fetched from the package where the class is located, and the '/' begins with the classpath root. It only constructs an absolute path through path, and eventually it gets the resources from the ClassLoader.

2. Class.getClassLoader.getResourceAsStream (String path): By default is obtained from the classpath root, path cannot start with '/' and is ultimately determined by ClassLoader get resources.


Org.dbunit.dataset.DataSetException:java.net.MalformedURLException Solution of Dbunit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.