如何配置模拟测试与DAO层Spring mvc?

时间:2016-06-22 04:03:47

标签: spring-mvc

我使用Spring mvc,使用hibernate和JPA配置,关系多对多,我在DAO中使用视图技术(jsp)测试方法(带控制器,服务)确定,当我转到写作时测试DAO课程,它失败了,我不知道,我哪里错了?,你能告诉我吗?谢谢 ! Student.java

@Entity
@Table(name = "Student")
public class Student {
@Id
@Column(name = "studentNumber", nullable = false)
private Integer studentNumber;
@Column(name = "studentName", nullable = false)
private String studentName;
@Column(name = "birthDay", nullable = false)
private String birthDay;
@Column(name = "birthPlace", nullable = false)
private String birthPlace;
@Column(name = "admissionDay", nullable = false)
private String admissionDay;
@Column(name = "score", nullable = false)
private Float score;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy =      "studentNumbers")
private Set<ClassRoom> classRooms = new HashSet<ClassRoom>();

ClassRoom.java

@Entity
 @Table(name = "ClassRoom")
 public class ClassRoom {
@Id
@Column(name = "classRoomID", unique = true, nullable = false)
private String classRoomID;
@Column(name = "classRoomName", nullable = false)
private String classRoomName;
@Column(name = "teacherName", nullable = false)
private String teacherName;
@JoinTable(name = "ClassRoom_Student", joinColumns = @JoinColumn(name = "classRoomID", referencedColumnName = "classRoomID", nullable = false, updatable = false) , inverseJoinColumns = @JoinColumn(name = "studentNumber", referencedColumnName = "studentNumber", nullable = false, updatable = false) )
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Student> studentNumbers = new HashSet<Student>();

DAO课程

@Repository
public class StudentDAOImpl implements StudentDAO {
@PersistenceContext(unitName = "default")
private EntityManager entityManager;
@Override
public boolean insert(Student student) {
    entityManager.persist(student);
    return true;
}
@Override
public boolean update(Student student) {
    entityManager.merge(student);
    return true;
}
@Override
public boolean delete(Integer studentNumber) {
     Student student = entityManager.find(Student.class, studentNumber);
     entityManager.remove(student);
    return true;
}
@Override
public Student getStudentByStudentNumber(Integer studentNumber) {
    Student student = entityManager.find(Student.class, studentNumber);
    return student;
}
@Override
public List<Student> getAllStudent() {
    return entityManager.createQuery("FROM Student").getResultList();
}
@Override
public List<String> getClassOfStudent(Integer studentNumber) {
    return entityManager
            .createQuery("SELECT cs.classRoomID FROM Class_Student cs WHERE cs.studentNumber=" + studentNumber)
            .getResultList();
}
@Override
public boolean deleteStudentClassStudent(Integer studentNumber) {
    Query query = entityManager
            .createQuery("DELETE FROM ClassRoom_Student cs WHERE cs.studentNumber = " + studentNumber);
    query.executeUpdate();
    return true;
}}

和mockTest

@ContextConfiguration(locations = { "classpath*:applicationContext.xml" })
  @RunWith(SpringJUnit4ClassRunner.class)
 public class Mockito {
 @Mock
 private static Student studentMock;
 @Mock
private static ClassRoom classMock;
@Autowired
private StudentDAO studentDAO;
@Autowired
private ClassRoomDAO classRoomDAO;
@Test
public void testCreateObject() {
    assertNotNull(studentDAO);
    assertNotNull(classRoomDAO);
}
@Test
public void insertTest() {
    Student student = new Student(111006, "ABC", "ABC", "ABC", "ABC", 10f);
    studentDAO.insert(student);
    List<Student> listStudent = studentDAO.getAllStudent();
    when(studentDAO.insert(student)).thenReturn(true);
    assertTrue(0 == listStudent.size());
}
@Test
public void testgetStudentByStudentNumber() {
    Student student = new Student();
  when(studentDAO.getStudentByStudentNumber(111000002)).thenReturn(student);
    assertTrue(student != null);
}
@Test
public void updateTest() {
    Student student = studentDAO.getStudentByStudentNumber(111002);
    student.setStudentName("new name");
    student.setAdmissionDay("new Day");
    student.setBirthDay("new Day");
    student.setBirthPlace("new Day");
    student.setScore(8f);
    when(studentDAO.update(student)).thenReturn(false);
    assertTrue(student == null);
}
@Test
public void deleteTest() {
    when(studentDAO.delete(111005)).thenReturn(true);
    Student stu = studentDAO.getStudentByStudentNumber(111005);
    assertTrue(stu != null);
}
@Test
public void getAllStudentTest() {
    List<Student> studentS = studentDAO.getAllStudent();
    assertTrue(studentS.size() != 0);
}
@Test
public void getStudentByIdTest() {
}
     

}

发生错误的

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ids.demo.dao.StudentDAO com.ids.demo.test.Mockito.studentDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ids.demo.dao.StudentDAO] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ids.demo.dao.StudentDAO] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 28 more

0 个答案:

没有答案