Тестирование Junit для работы в спящем режиме. - PullRequest
0 голосов
/ 31 марта 2019

Я нашел некоторые учебники в Интернете и некоторые ресурсы по Stackoverflow, но я не могу перевести эту логику в свое приложение. Я новичок в Junit testing. У меня есть 3 класса, User, UserServiceH2 и UserController

Класс пользователя

@Id
@GeneratedValue
private int id;
@Column(name = "username")
private String username;    //username of the user
@Column(name = "first_name")
private String firstname;   //first name of the user
@Column(name = "last_name")
private String lastname;    //last name of the user
@Column(name = "doj")
private Date dateOfJoining; //date of joining of the user

/** This is the getter method for username of the user.using this we can get user's username
 *
 * @return string
 */

public String getUsername() {
    return username;
}

/** This is the setter method for username of the user. We can set the user's username
 *
 * @return void
 */

public void setUsername(String username) {
    this.username = username;
}

/** We can get the first name of the user.
 *
 * @return string
 */

public String getFirstname() {
    return firstname;
}

/** We can set the user's first name
 *
 * @return void
 */

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

/** We can get the user's last name
 *
 * @return string
 */

public String getLastname() {
    return lastname;
}

/** We can set the last name of the user
 *
 * @return void
 */

public void setLastname(String lastname) {
    this.lastname = lastname;
}

/** We can get joining date of user
 *
 * @return Date
 */

public Date getDateOfJoining() {
    return dateOfJoining;
}

/** We can set joining date of user
 *
 * @return void
 */

public void setDateOfJoining(Date dateOfJoining) {
    this.dateOfJoining = dateOfJoining;
}

UserController

/**
 * creating user into database
 * @param
 */

public void createUser() {
    User user = new User();
    UserServiceH2 serviceH2 = new UserServiceH2();
    serviceH2.createUser(user);
}

/**
 * retrieving all the instances from database
 * @return
 */

public void getAllUsers() {
    UserServiceH2 serviceH2 = new UserServiceH2();
    List<User> allUsers = serviceH2.getAllUsers();
    for(int i = 0; i < allUsers.size(); i++) {
        System.out.println( allUsers.get(i)
        );
    }
}

/**
 * Retrieve a resource based on id and update its firstname, lastname or username
 * @param
 */

public void updateUser() {
    User user = new User();
    UserServiceH2 serviceH2 = new UserServiceH2();
    serviceH2.updateUser(user);
}

/**
 * delete a resource from database based on given id
 * @param
 */

public void deleteUser() {
    User user = new User();
    UserServiceH2 serviceH2 = new UserServiceH2();
    serviceH2.deleteUser(user);
}

UserServiceH2

/**
 * creating user into database
 * @param
 */
public void createUser(User u) {
    //setting values for user
    u.setFirstname("Qasim");
    u.setLastname("Ali");
    u.setUsername("DjBravo");
    u.setDateOfJoining(new Date());
    SpecialUser specialUser = new SpecialUser();
    specialUser.setSpecialDiscount(50.00);
    //object of student user
    StudentUser studentUser = new StudentUser();
    studentUser.setUniversity("University of Bolzano");
    SessionFactory sessionFactory   = new Configuration().configure().addAnnotatedClass(User.class).buildSessionFactory();
    Session session                 = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.save(u);
    session.save(specialUser);
    session.save(studentUser);
    session.getTransaction().commit();
    session.close();
}

/**
 * retrieving all the instances from database
 * @return List
 */

public List<User> getAllUsers() {
    SessionFactory sessionFactory   = new Configuration().configure().addAnnotatedClass(User.class).buildSessionFactory();
    Session session                 = sessionFactory.openSession();
    session.beginTransaction();
    //retrieve all the user instances from database
    List<User> users                = session.createQuery("FROM User").list();
    return users;
}

/**
 * Retrieve a resource based on id and update its firstname, lastname or username
 * @param u
 */
public void updateUser(User u) {
    int userId = 2;
    SessionFactory sessionFactory   = new Configuration().configure().addAnnotatedClass(User.class).buildSessionFactory();
    //now get a new session and start transaction
    Session session                 = sessionFactory.getCurrentSession();
    session.beginTransaction();
    //retrieve student based on the id
    System.out.println("\n Getting Student with id " + userId);
    User myUser = session.get(User.class, userId);
    System.out.println("Updating Student ");
    myUser.setUsername("Sarfraz");

    //commit the transaction
    session.getTransaction().commit();
    System.out.println("Done");
    session.close();
}

/**
 * delete a resource from database based on given id
 * @param u
 */
public void deleteUser(User u) {
    int userId = 6;
    SessionFactory sessionFactory   = new Configuration().configure().addAnnotatedClass(User.class).buildSessionFactory();
    //now get a new session and start transaction
    Session session                 = sessionFactory.getCurrentSession();
    session.beginTransaction();
    //retrieve student based on the id
    System.out.println("\n Getting Student with id " + userId);
    User myUser = session.get(User.class, userId);

    //delete student
    System.out.println("Deleting Student " + myUser);
    session.delete(myUser);

    //commit the transaction
    session.getTransaction().commit();
    System.out.println("Done");
    session.close();
}

Пожалуйста, определите неправильную практику и расскажите лучшие практики. Как я могу написать логику для Junit testing

TestUserController

@Test
void createUser(User user) {
    user.setUsername("Oscar");
    user.setFirstname("Darren");
    user.setLastname("Sammy");
    user.setDateOfJoining(new Date());
    SessionFactory sessionFactory   = new Configuration().configure().addAnnotatedClass(User.class).buildSessionFactory();
    Session session                 = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.save(user);
    assertEquals(1,);
}

@Test
void getAllUsers() {
}

@Test
void updateUser() {
}

@Test
void deleteUser() {
}
...