这是我的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<context:annotation-config />
<context:component-scan base-package="com" />
<context:property-placeholder location="/config.properties" />
<bean id="myDataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
这是我的Dao:
package com.dao.impl;
import com.dao.UniversityBasicInformationDao;
import com.message.Message;
import com.message.imp.UniversityMessage;
import com.model.UniversityBasicInformation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UniversityBasicInformationImp implements
UniversityBasicInformationDao {
@Autowired
private SessionFactory sessionFactory;
private static Logger logger;
public Message[] getAll() {
Session session = sessionFactory.getCurrentSession();
logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
List list = session.createQuery("FROM UniversityBasicInformation ").list();
session.close();
Message[] message = new Message[list.size()];
for (int i = 0; i < list.size(); i++) {
UniversityBasicInformation university = (UniversityBasicInformation) list.get(i);
UniversityMessage universityMessage = new UniversityMessage(university);
message[i] = universityMessage;
}
logger.info(message[0].getJson());
return message;
}
public Message findByName(String name) {
Session session = sessionFactory.getCurrentSession();
logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
List list = session.createQuery("FROM UniversityBasicInformation " +
"WHERE universityName = ?").setParameter(0, name).list();
session.close();
return new UniversityMessage((UniversityBasicInformation) list.get(0));
}
public Message findByID(int id) {
Session session = sessionFactory.getCurrentSession();
logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
List list = session.createQuery("FROM UniversityBasicInformation " +
"WHERE universityId = ?").setParameter(0, id).list();
session.close();
return new UniversityMessage((UniversityBasicInformation) list.get(0));
}
public void add(UniversityBasicInformation university) {
Session session = sessionFactory.getCurrentSession();
logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
Transaction tx = session.beginTransaction();
try {
session.save(university);
tx.commit();
} catch (Exception e) {
logger.error("UniversityBasicInformationDao add Exception: " + e.toString());
} finally {
session.close();
}
}
public void remove(UniversityBasicInformation university) {
Session session = sessionFactory.getCurrentSession();
logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
Transaction tx = session.beginTransaction();
try {
session.delete(university);
tx.commit();
} catch (Exception e) {
logger.error("UniversityBasicInformationDao delete Exception: " + e.toString());
} finally {
session.close();
}
}
public void update(UniversityBasicInformation university) {
Session session = sessionFactory.getCurrentSession();
logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
Transaction tx = session.beginTransaction();
try {
session.update(university);
tx.commit();
} catch (Exception e) {
logger.error("UniversityBasicInformationDao delete Exception: " + e.toString());
} finally {
session.close();
}
}
}
这是我的testDao:
package com.dao.impl;
import com.dao.UniversityBasicInformationDao;
import com.message.Message;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:applicationContext.xml")
class UniversityBasicInformationImpTest {
@Autowired
private UniversityBasicInformationDao universityBasicInformationDao;
@Test
void getAll() {
assertFalse(universityBasicInformationDao == null);
Message[] message = universityBasicInformationDao.getAll();
assertEquals("{\"university_id\":\"1\"," +
"\"university_address_line_1\":\"1410 NE Campus Parkway \"," +
"\"university_address_line_2\":null,\"university_name\":\"University of Washington\"," +
"\"university_profile_image_id\":\"com.model.ImageObject@2802756f\"}",
message[0].getJson());
}
}
事情是,当我运行tomcat服务器时它运行良好,但在运行测试时,它会抛出一个NullPointerException()。我已经在线查看了很多文档,还有很多其他问题在stackoverflow中,但它对我来说不起作用。我被困在这里将近一整天,请救我。