简体   繁体   中英

org.dbunit.dataset.NoSuchTableException: UserTest

I have found some example of how to use dbunit here, from the junitbook2 example

I have tried and It works, so I have try to use It in my project; my project is maven based, and to use the database I use a reference to an external jar that create my table's bean with "ant run". So in my maven project I have create 1 Interface (UserDao) in my src/main/java + other class in src/test/java like the example in the book.

UserDao interface :

import java.sql.SQLException;
import com.mydb.UserTest;
public interface UserDao {

  /**
   * Insert an user in the database.
   * 
   * @param user user to be inserted
   * @return if id the inserted user
   */
  long addUser( UserTest user ) throws SQLException;
  UserTest getUserById( long id ) throws SQLException;
}

AbstractDbUnitTestCase:

import static org.junit.Assert.*;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ReplacementDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.ext.hsqldb.HsqldbConnection;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public abstract class AbstractDbUnitTestCase {

  protected static UserDaoJdbcImpl dao = new UserDaoJdbcImpl();
  protected static Connection connection;
  protected static HsqldbConnection dbunitConnection;

  @BeforeClass
  public static void setupDatabase() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:mysql://mydb/mydb","user","password");
    if (connection != null) 
        System.out.println("Connected to the database");
    dbunitConnection = new HsqldbConnection(connection,null);
    dao.setConnection(connection);
  }

  @AfterClass
  public static void closeDatabase() throws Exception {
    if ( dbunitConnection != null ) {
      dbunitConnection.close();
      dbunitConnection = null;
    }
  }

  public static IDataSet getDataSet(String name) throws Exception {
    InputStream inputStream = AbstractDbUnitTestCase.class.getResourceAsStream(name);
    assertNotNull("file " + name + " not found in classpath", inputStream );
    Reader reader = new InputStreamReader(inputStream);
    FlatXmlDataSet dataset = new FlatXmlDataSet(reader);
    return dataset;
  }

  public static IDataSet getReplacedDataSet(String name, long id) throws Exception {
    IDataSet originalDataSet = getDataSet(name);
    return getReplacedDataSet(originalDataSet, id);
  }

  public static IDataSet getReplacedDataSet(IDataSet originalDataSet, long id) throws Exception {
    ReplacementDataSet replacementDataSet = new ReplacementDataSet(originalDataSet);
    replacementDataSet.addReplacementObject("[ID]", id);
    replacementDataSet.addReplacementObject("[NULL]", null);
    return replacementDataSet;
  }

}

UserDaoJdbcImplTest --> where is the test

import static org.junit.Assert.*;
import static com.test.EntitiesHelper.*;


import org.dbunit.Assertion;
import org.dbunit.dataset.IDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Ignore;
import org.junit.Test;

import com.mydb.UserTest;

public class UserDaoJdbcImplTest extends AbstractDbUnitTestCase {

  @Test
  public void testGetUserById() throws Exception {
    IDataSet setupDataSet = getDataSet("/user.xml");
    DatabaseOperation.CLEAN_INSERT.execute(dbunitConnection, setupDataSet);
    UserProvaTest user = dao.getUserById(1);
    assertNotNull( user);
    assertEquals( "Jeffrey", user.getFirstName() );
    assertEquals( "Lebowsky", user.getLastName() );
    assertEquals( "ElDuderino", user.getUsername() );
  }

  @Test @Ignore("fails if run together with others")
  public void testAddUser() throws Exception {
    UserProvaTest user = newUser();
    long id = dao.addUser(user);
    assertTrue(id>0);
    IDataSet expectedDataSet = getDataSet("/user.xml");
    IDataSet actualDataSet = dbunitConnection.createDataSet();
    Assertion.assertEquals( expectedDataSet, actualDataSet );
  }

  @Test
  public void testAddUseIgnoringId() throws Exception {
    IDataSet setupDataSet = getDataSet("/user.xml");
    DatabaseOperation.DELETE_ALL.execute(dbunitConnection, setupDataSet);
    UserProvaTest user = newUser();
    long id = dao.addUser(user);
    assertTrue(id>0);
    IDataSet expectedDataSet = getDataSet("/user.xml");
    IDataSet actualDataSet = dbunitConnection.createDataSet();
    Assertion.assertEqualsIgnoreCols( expectedDataSet, actualDataSet, "users", new String[] { "id" } );
  }

  @Test
  public void testGetUserByIdReplacingIds() throws Exception {
    long id = 42;
    IDataSet setupDataset = getReplacedDataSet("/user-token.xml", id );
    DatabaseOperation.INSERT.execute(dbunitConnection, setupDataset);
    UserProvaTest user = dao.getUserById(id);
    assertUser(user);
  }

  @Test
  public void testAddUserReplacingIds() throws Exception {
    IDataSet setupDataSet = getDataSet("/user-token.xml");
    DatabaseOperation.DELETE_ALL.execute(dbunitConnection, setupDataSet);
    UserProvaTest user = newUser();
    long id = dao.addUser(user);
    assertTrue(id>0);
    IDataSet expectedDataSet = getReplacedDataSet(setupDataSet, id );
    IDataSet actualDataSet = dbunitConnection.createDataSet();
    Assertion.assertEquals( expectedDataSet, actualDataSet );
  }

}

My Bean UserTest is created in this way :

@Entity (name="mydb_UserTest")@Table(name="UserTest"
    ,catalog="mydb" )

and the user.xml file :

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <mydb_UserTest id="1" username="ElDuderino" first_name="Jeffrey" last_name="Lebowsky" />
</dataset>

But when I tried to execute the test I have this error:

testGetUserById(com.test.UserDaoJdbcImplTest) Time elapsed: 0.176 sec <<< ERROR! org.dbunit.dataset.NoSuchTableException: UserTest

I have see that :

  1. the connection to the database is correct
  2. but no matter what I wrote into the user.xml file, for example User1111Test instead of UserTest, the error is the same(like org.dbunit.dataset.NoSuchTableException: User1111Test); I have also try to use schema.UserTest, that is suggested here but the error is the same NoSuchTableException: schema.UserTest.
  3. The error is in this point :

    DatabaseOperation.CLEAN_INSERT.execute(dbunitConnection, setupDataSet);

I am doing something wrong?

You have to add FEATURE_QUALIFIED_TABLE_NAMES property:

DatabaseConfig config = dBConn.getConfig();
config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

Place it in your setupDatabase method.

In your dataset you must add the schema name:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
 <SCHEMA.USERTEST ID="1" USERNAME="ELDUDERINO" FIRST_NAME="JEFFREY" LAST_NAME="LEBOWSKY" />
</dataset>

Note the dataset in uppercase.

Hope this helps.

Based on this info:

@Entity (name="mydb_UserTest")@Table(name="UserTest"
    ,catalog="mydb" )

the correct dbUnit file table name is:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <mydb.UserTest id="1" username="ElDuderino" first_name="Jeffrey" last_name="Lebowsky" />
</dataset>

Or without the schema name if it's the default schema for the database user (usually when the schema name matches the user name):

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <UserTest id="1" username="ElDuderino" first_name="Jeffrey" last_name="Lebowsky" />
</dataset>

Finally, I got the solution...

I changed my table name in DB from UserTest to USERTEST , and I use this user.xml :

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <USERTEST id="1" username="ElDuderino" first_name="Jeffrey" last_name="Lebowsky" />
</dataset>

I think, in my case, there is no alternatives because the 2 properties:

config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
config.setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, true);

Return this info, so they don't work:

[main] INFO org.dbunit.database.DatabaseConfig - Unknown property 'http://www.dbunit.org/features/qualifiedTableNames'. Cannot validate the type of the object to be set. Please notify a developer to update the list of properties.  
[main] INFO org.dbunit.database.DatabaseConfig - Unknown property 'http://www.dbunit.org/features/caseSensitiveTableNames'. Cannot validate the type of the object to be set. Please notify a developer to update the list of properties.

UPDATE: The solution to avoid the info/error of FEATURE_QUALIFIED_TABLE_NAMES is to use

dbunitConnection.getConfig().setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true); dbunitConnection.getConfig().setFeature(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, true);

instead of setProperty ; and change HsqldbConnection dbunitConnection to MySqlConnection dbunitConnection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM