Junit DB Test results in Incompatible data types error
04:13 20 Aug 2018

I have got a problem considering running a JUnit test for the SQL Updates.

I have got my DAO class and DAO test class similar to:

public class generalDao {
//example of getter 
    public Account getAccountById(String id) {
        return getEntityManager().createQuery("SELECT ac FROM Account ac WHERE ac.accountId = :ID", Account.class).setParameter("ID",id);
    }

//example of update
    public void incrementSubscriberCount(String mnt, Date vf, List accs){
    getEntityManager().createQuery("UPDATE Account ac SET ac.subs = ac.subs+1 WHERE ac.date < :DATE AND ac.mnt = :MNT AND ac.accountId IN (:accs)").setParameter("DATE",vf).setParameter("MNT", mnt).setParameter("accs",accs).executeUpdate();       
    }
}

--

public class generalDaoTest extends generalDao{
    private static EntityManager em;

    @Test
    public void getAccountById(){
    super.getAccountById("1");
    }

    @Test
    public void incrementSubscriberCount(){
    em.getTransaction().begin();
    super.incrementSubscriberCount("5", new Date(), Collections.singletonList("abc"));
    em.getTransaction().rollback();
    }
}

Also, there are different (longer) and additional methods in the actual classes, these mentioned here are for demonstrative purposes.

My problem there is, that running the test on incrementSubscriberCount() (or rather any method that runs update query) which should just validate the SQL will get me following error when test reaches the executeUpdate():

WARN  SQL Error: -5562, SQLState: 42562
ERROR  incompatible data types in combination in statement .......
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement

Now... I checked all the types, because that is the obvious thing to do with exception sounding like this. But all of them are OK.

Also, I tried turning the Update into Select, while keeping the "Where" part the same, just to see if my parameters are wrong.

public void incrementSubscriberCount(String mnt, Date vf, List accs){
    getEntityManager().createQuery("SELECT ac FROM Account ac 
    WHERE ac.date < :DATE AND ac.mnt = :MNT AND ac.accountId IN (:accs)").setParameter("DATE",vf)
    .setParameter("MNT", mnt).setParameter("accs",accs).getResultList();       
    }

And this select works fine. The only thing failing is the executeUpdate() method in update.

Another thing that bugs my mind is, that the methods actually work. I mean... when deployed and used, there are no errors. So there has to be some silly error in the test I cannot find.

My test has no assertions, therefore it does not matter if the query finds no row to update. I just need it to pass without crashing.

Project uses Hibernate and the target DB is Oracle.

java hibernate junit