Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何使用DAO层Spring mvc配置模拟测试?_Spring Mvc - Fatal编程技术网

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

Spring mvc 如何使用DAO层Spring mvc配置模拟测试?,spring-mvc,Spring Mvc,我使用SpringMVC,使用hibernate和JPA配置,关系多对多,我在DAO中使用view technologies(jsp)测试方法(带有控制器、服务)好的,当我开始为DAO类编写测试时,它失败了,我不知道,我哪里错了?请告诉我?谢谢你! Student.java @Entity @Table(name = "Student") public class Student { @Id @Column(name = "studentNumber", nullable = false) pr

我使用SpringMVC,使用hibernate和JPA配置,关系多对多,我在DAO中使用view technologies(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>();

@Repository StudentDao不在应用程序上下文中,请检查xml文件中的上下文扫描,并检查persistentUnit名称是否正确@Repository StudentDao不在应用程序上下文中,请检查xml文件中的上下文扫描,并检查persistentUnit名称是否正确
@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>();
@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;
}}
@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