dimanche 27 décembre 2015

Java Mock data base for testing Spring application

I have made simple application for study perpose and i want to write some unit/intagration tests. I read some information about that i can mock data base insted of create new db for tests. I will copy the code which a write. I hope that some one will explain me how to mock database.

public class UserServiceImpl implements UserService {

    @Autowired
    private UserOptionsDao uod;

    @Override
    public User getUser(int id) throws Exception {
        if (id < 1) {
            throw new InvalidParameterException();
        }
        return uod.getUser(id);
    }

    @Override
    public User changeUserEmail(int id, String email) {
        if (id < 1) {
            throw new InvalidParameterException();
        }

        String[] emailParts = email.split("@");
        if (emailParts[0].length() < 5) {
            throw new InvalidParameterException();
        } else if (!emailParts[1].equals("email.com")) {
            throw new InvalidParameterException();
        }

        return uod.changeUserEmail(id, email);
    }

This above i a part of the code that i want to test with the mock data base.

Aucun commentaire:

Enregistrer un commentaire