samedi 19 septembre 2020

When writing the unit test, which one should I prefer as the expected value?

I'm developing a project. The subject of this project, companies send message to users. Each company has a message limit and the system throws an exception based on the language chosen by the company when the message limit is exceeded.

I wrote unit test for the exception.

// given
Company company = new Company("Comp1", 2); // constructor (company name, language)  **  2 -> EN
User user = new User("User1");
Email email = new Email("Email Test", "Test");
int emailLimit = company.getEmailLimit();
    
// when
for (int i = 0; i < emailLimit; i++) {
     company.SendEmail(email, user);
}
Throwable throwable = catchThrowable(() -> company.SendEmail(email, user));

// then
assertThat(throwable).isInstanceOf(MessageLimitException.class);

I also want to check the message content.

There is a class named "ErrorMessages" that manages the content of the error message.

public class ErrorMessages {

   private static String[] messageLimitErrorMessage = {
        "Message Limit Error",   // 0 -> default
        "Mesaj limiti aşıldı",   // 1 -> TR
        "Message limit exceeded" // 2 -> EN
   };

   public static String messageLimitException(int languageIndex) {
        return messageLimitErrorMessage[languageIndex];
   };

}

Which one should I prefer as the expected value?

// Option 1
assertThat(throwable).hasMessage(ErrorMessages.messageLimitException(company.getLanguage()));

// or

// Option 2
assertThat(throwable).hasMessage("Message limit exceeded");

Both are correct but which one should I prefer for the accuracy of the test, Option 1 or 2 ?

Thanks for your answer in advance.

Aucun commentaire:

Enregistrer un commentaire