[英]Spring unit test not getting the updated values from database
我使用簡單的彈簧配置,使用普通的彈簧jdbc並安裝了dao。 我為dao層添加了一個測試用例,它具有以下功能。 -獲取設置-測試記錄數和值-用新值更新設置-測試更新后的值。
我無法斷言更新后的值,以下是我遇到的錯誤。 不知道出什么問題了。
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.386 sec <<< FAILURE!
updateTest(com.sims.service.SettingsServiceTests) Time elapsed: 1.086 sec <<< FAILURE!
org.junit.ComparisonFailure: expected:<[My Org]> but was:<[New Org1]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.abc.service.SettingsServiceTests.updateTest(SettingsServiceTests.java:57)
誰能讓我知道配置中有什么問題。 我添加了applicationContext.xml,Dao和dao的單元測試。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- import the dataSource definition -->
<import resource="datasource-config.xml" />
<context:component-scan base-package="com.abc" />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:property-placeholder location="classpath:spring/data-access.properties"
system-properties-mode="OVERRIDE" />
<tx:annotation-driven />
<!-- Transaction manager for a single JDBC DataSource (alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
@Repository
public class SettingsDaoImpl extends BaseDao{
private static final class SettingMapper implements RowMapper<Settings> {
public SettingMapper() {
super();
}
@Override
public Settings mapRow(final ResultSet rs, final int rowNum) throws SQLException {
final Settings settings= new Settings();
settings.setId(rs.getLong("id"));
settings.setOrgName(rs.getString("org_name"));
settings.setOrgAddress(rs.getString("org_address"));
settings.setOrgPhone(rs.getString("org_phone"));
settings.setLanguage(rs.getString("language"));
settings.setTimeZone(rs.getString("time_zone"));
settings.setCountry(rs.getString("country"));
settings.setCurrencyType(rs.getString("currency"));
return settings;
}
}
@Transactional(readOnly = true)
public Settings getSettings(){
final String sql = "select * from settings";
final Settings student = new Settings();
final SqlParameterSource paramSource = new BeanPropertySqlParameterSource(
student);
final List<Settings> result = this.getNamedParameterJdbcTemplate().query(
sql, paramSource, new SettingMapper());
if (result != null && !result.isEmpty()) {
return result.get(0);
}
return null;
}
@Transactional(readOnly = false)
public Settings saveSetting(Settings settings) {
Settings tSettings = null;
if (settings.isNew()) {
tSettings = insert(settings);
} else {
tSettings = update(settings);
}
return tSettings;
}
public Settings insert(Settings settings){
final String sql = "insert into settings (org_name, org_address, org_phone, language, time_zone, country, currency) "
+" values (:orgName, :orgAddress, :orgPhone, :language, :timeZone, :country, :currency)";
final GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
final String[] keyColumnNames = { "id" };
final SqlParameterSource paramSource = new BeanPropertySqlParameterSource(
settings);
final int count = this.getNamedParameterJdbcTemplate().update(sql,
paramSource, generatedKeyHolder, keyColumnNames);
settings.setId(generatedKeyHolder.getKey().longValue());
return settings;
}
public Settings update(final Settings settings) throws DataAccessException {
final StringBuilder updateClause = new StringBuilder();
SettingDb.getUpdateSql(updateClause);
final String sql = "update settings set org_name:=orgName, org_address:= orgAddress, " +
org_phone:=orgPhone, language:=language, time_zone:=timeZone, country:=country, currency:=currency where id=:id";
final GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
final String[] keyColumnNames = { "id" };
final SqlParameterSource paramSource = new BeanPropertySqlParameterSource(
settings);
final int count = this.getNamedParameterJdbcTemplate().update(sql,
paramSource, generatedKeyHolder, keyColumnNames);
return settings;
}
}
@Transactional(propagation = Propagation.NESTED)
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SettingsDAOTests {
@Autowired
private SettingsDaoImpl settingsDaoImpl;
@Test
public void testSettings(){
Settings settings = settingsDaoImpl.getSettings();
Assert.assertNotNull(settings);
String orgName = "New Org";
String orgAddress = "New Org Ad";
String phoneNumber = "Ph1";
String language = "Hindi";
String timeZone = "GMT";
String country = "US";
String currency = "Dollar";
settings.setCountry(country);
settings.setOrgAddress(orgAddress);
settings.setOrgName(orgName);
settings.setOrgPhone(phoneNumber);
settings.setLanguage(language);
settings.setTimeZone(timeZone);
settings.setCurrencyType(currency);
settingsDaoImpl.saveSetting(settings);
Settings settings1 = settingsDaoImpl.getSettings();
Assert.assertEquals(orgName, settings1.getOrgName());
Assert.assertEquals(orgAddress, settings1.getOrgAddress());
Assert.assertEquals(phoneNumber, settings1.getOrgPhone());
Assert.assertEquals(timeZone, settings1.getTimeZone());
Assert.assertEquals(country, settings1.getCountry());
Assert.assertEquals(currency, settings1.getCurrencyType());
String orgName1 = "New Org1";
String orgAddress1 = "New Org Ad1";
String phoneNumber1 = "Ph11";
String language1 = "Hindi1";
String timeZone1 = "GMT1";
String country1 = "US1";
String currency1 = "Dollar1";
settings.setCountry(country1);
settings.setOrgAddress(orgAddress1);
settings.setOrgName(orgName1);
settings.setOrgPhone(phoneNumber1);
settings.setLanguage(language1);
settings.setTimeZone(timeZone1);
settings.setCurrencyType(currency1);
settingsDaoImpl.saveSetting(settings);
Settings settings2 = settingsDaoImpl.getSettings();
Assert.assertEquals(orgName, settings2.getOrgName());
Assert.assertEquals(orgAddress, settings2.getOrgAddress());
Assert.assertEquals(phoneNumber, settings2.getOrgPhone());
Assert.assertEquals(timeZone, settings2.getTimeZone());
Assert.assertEquals(country, settings2.getCountry());
Assert.assertEquals(currency, settings2.getCurrencyType());
}
}
Spring將在您使用時回滾數據庫更新:
defaultRollback=true
我無法回答,我正在回答。 我建議您參考以下鏈接:-http: //www.journaldev.com/2603/spring-transaction-management-example-with-jdbc
我覺得您遇到了某種異常,請在調用數據庫查詢時放入try catch塊。 嘗試深入研究堆棧跟蹤,您將找到解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.