lundi 22 juin 2015

Java Junit test

I want to write a Junit test to the DAO. The project work itself. All classes and methods work. All data from the test correctly. Why is it bad?

location of the files in the project:

src/test/java - com.epam.edu.jtc.test -CourseTest.java
src/test/resources- test-context.xml, application-context.xml
src/main/resources- test-context.xml, application-context.xml

Exception is --- Could not autowire field: private com.epam.edu.jtc.dao.CoursesDAOImpl com.epam.edu.jtc.test.CourseTest.coursesDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.epam.edu.jtc.dao.CoursesDAOImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Why does not it work?

CourseTest.java

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:test-context.xml" 
 } )
 //@Transactional
 //@TransactionConfiguration(defaultRollback=true)
 public class CourseTest
{
@Autowired 
private CoursesDAOImpl coursesDao;

@Test
public void testFindCourseById()
{
    Course course = coursesDao.findCourseById(322);

    Assert.assertEquals("Python", course.getName());
    Assert.assertEquals("Development Manager", course.getCategory());
    Assert.assertEquals("python.com", course.getLinks());
    return;
 }

test-context.xml

<mvc:annotation-driven />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"
    p:driverClassName="org.h2.Driver"
    p:url="jdbc:h2:tcp://localhost:9092/~/QWE;INIT=create schema if not exists QWE\;"
    p:username="sa"
    p:password=""
     />
 <bean id="coursesDao" class="com.epam.edu.jtc.dao.CoursesDAOImpl">
    <property name="dataSource" ref="dataSource"/>
</bean>

CoursesDAOImpl.java

 @Repository
 public class CoursesDAOImpl implements CoursesDAO {

 @Autowired
 private SessionFactory sessionFactory;

 public Course findCourseById(Integer id) {

 Session session = sessionFactory.getCurrentSession();
 session.beginTransaction();
 Course course = (Course) session.get(Course.class, id);
 session.getTransaction().commit();

 return course;
 }

Aucun commentaire:

Enregistrer un commentaire