There is a small console application, which, depending on the time and region of the user, gives to the console a greeting of different text. Log4j is also used. Greetings are set by the Resources Bundle. I need your help in writing Unit Tests ... Namely, examples and which parameters to check. In my head only stupid examples, so I need your help in fantasy) Thank you in advance. Link to the githaba project. https://github.com/dimsdale/TestWork

  • 3
    the fact of the matter is that examples, no matter how stupid they are, should be posted. True, they will be judged on what matter is connected with them. If there is no question, no answer. - Roman C
  • Give an example of the source code. In short, you need to test the logic, which "depending on time and region" selects the desired text. - andreycha
  • @andreycha Link to the githab with the project attached. You can read. - Dmitriy Smirnov
  • one
    @ Dmitry, instead of a link, it was worth adding the code you want to test directly into the question - Grundy

1 answer 1

Your main logic lies in the class Greeting - and it needs to be tested. In the welcomeUserByLocalization() method, you have quite specific cases.

 public class Greeting { private static final Logger log = LogManager.getLogger(Greeting.class); private static final String GOODNIGHT = "GoodNight"; private static final String GOODMORNING = "GoodMorning"; private static final String GOODDAY = "GoodDay"; private static final String GOODEVENING = "GoodEvening"; public String welcomeUserByLocalization (Date date, ResourceBundle resourceBundle){ int hour = date.getHours(); log.info(String.format("Input date is: '%s', resourceBundle = '%s'" , new SimpleDateFormat("HH:mm:ss").format(date), resourceBundle.getLocale())); if (hour >= 23 || hour >= 0 && hour <= 5){ return resourceBundle.getString(GOODNIGHT); } else if (hour >= 6 && hour <= 8){ return resourceBundle.getString(GOODMORNING); } else if (hour >= 9 && hour <= 18){ return resourceBundle.getString(GOODDAY); } else if (hour >=19 && hour <=22) { return resourceBundle.getString(GOODEVENING); } else return null; } } 

Looking into the test folder, I saw that you had already written the tests. A few comments on them:

  • Some tests now do not really check the logic of the method welcomeUserByLocalization() . For example, the welcomeUserTest2 test only checks that a string has returned for a given time. And you need to check that the string is returned by the key "GoodNight". To do this, you either compare the return value with the value from the ResourceBundle that you create in the test, or mock the ResourceBundle and specify your return value.
  • For each of the ranges, it is advisable to check both the boundary conditions (since you are not very strict) and the value within the range.
  • Name the tests according to your case studies .

I would not test all other things ( MessageInit , Locale ), at least for now . They are very trivial and tests essentially boil down to testing JDK code.