I have been trying to test the DAO layer of an application, however I keep getting this error.
Here is the Test class
RegionDAOTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.teamw.ibt.dao.impl.RegionDAOImpl;
import com.teamw.ibt.model.Region;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(locations = "classpath:app-context.xml")
public class RegionDAOTest extends AbstractDAOTest {
@Autowired
private JdbcTemplate jdbcTemplate;
private RegionDAOImpl regionDAOImpl;
@Before
public void setUp(){
regionDAOImpl = new RegionDAOImpl();
regionDAOImpl.setJdbcTemplate(jdbcTemplate);
}
@After
public void tearDown(){
}
@Test
public void testContext() {
assertNotNull(regionDAOImpl);
}
@Test
public void FindByCodeTest() {
Region region = regionDAOImpl.findByCode("1");
assertEquals('1', region.getCode());
assertEquals("Region 1", region.getName());
}
}
RegionDAOImpl.java
@Repository
public class RegionDAOImpl implements RegionDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
public RegionDAOImpl() {
}
@Autowired
public RegionDAOImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void insert(Region region) {
String query = "INSERT INTO IBT.REGION (code, name) VALUES ('" + region.getCode() + "','" + region.getName() + "');";
jdbcTemplate.update(query);
}
public Region findByCode(String code) {
String query = "SELECT * FROM IBT.REGION WHERE CODE = '" + code + "';";
return jdbcTemplate.query(query, new ResultSetExtractor<Region>() {
@Override
public Region extractData(ResultSet rs) throws SQLException, DataAccessException {
if (rs.next()) {
return new Region(
rs.getString("code"),
rs.getString("name")
);
}
return null;
}
});
}
}
app-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/QEDs1e ">
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem"/>
<property name="username" value="SA"/>
<property name="password" value="test"/>
</bean>
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="regionDAOImpl" name="regionDAOImpl" class="com.teamw.ibt.dao.impl.RegionDAOImpl">
<constructor-arg ref="jdbcTemplate"/>
</bean>
</beans>
As far as I know, I have set up everything correctly, but for some reason it says dataSource is not defined.
Aucun commentaire:
Enregistrer un commentaire