Like a unit test class with multiple boolean fields

Sorry if this is a stupid question. I would like the unit test to have the next class (more for the class, but I split it up to illustrate my question).

A class consists of several Boolean fields that are assigned in the constructor.

When I test the module, how can I check the parameters, the correct class field is assigned, for example. ans1 → mIsOption1Ans?

For example, if I claim that all the fields of a class are “true”, this will not identify if another developer accidentally replaced the assignment in the constructor and assigned a parameter to the wrong class field. I would like to check that "ans1" is always assigned to "mIsOption1Ans" etc. Etc.

public class MultipleChoiceQuizAnswer {
   private Boolean mIsOption1Ans,mIsOption2Ans,mIsOption3Ans,mIsOption4Ans;

   public QuizAnswer (Boolean ans1,Boolean ans2, Boolean ans3,Boolean ans4) {
         mIsOption1Ans = ans1;
         mIsOption2Ans = ans2;
         mIsOption3Ans = ans3;
         mIsOption4Ans = ans4;
    }
}
+4
source share
2

, , , 1 boolean - true, rest - false.

, :

QuizAnswer firstTrue = QuizAnswer(true, false, false, false);
assertTrue(firstTrue.isFirstAnswer());
// Then for next:
QuizAnswer secondTrue = QuizAnswer(false, true, false, false);
assertTrue(secondTrue.isSecondAnswer());
// Etc. You could also check if all other answers are false
+3

. . QuizAnswer, . . , , :

@Test
public void first_option_is_the_answer_when_ans1_flag_is_true() {
    QuizAnswer answer = new QuizAnswer(true, false, false, false);
    boolean firstOptionIsAnswer = answer.isNthOptionTheAnswer(1);
    assertTrue(firstOptionIsAnswer);
}

JUnit Parameterized, , .

+3

Source: https://habr.com/ru/post/1695473/


All Articles