How to unit test a class with multiple boolean fields

Ian Hamilton :

Sorry if this is a dumb question. I'd like to unit test the following class (there's more to the class but I stripped it down to illustrate my question).

The class consists of multiple boolean fields that get assigned in the constructor.

When I'm unit testing how can I test the parameters are assigned to the correct class field e.g. ans1 -> mIsOption1Ans ?

For example if I assert that all the class fields are "true" this will not identify if another developer has accidentally swapped the assignment in the constructor and assigned a parameter to the wrong class field. I'd like to test that "ans1" always gets 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;
    }
}
FilipRistic :

This is really obvious thing but if you really want to test it you can test for each situation where 1 boolean is true and rest are false.

So for example:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How To Unit Test Multiple Instances Of Class In xUnit

How should I unit test multiple required fields in C#?

How do I test interactions with private fields of a class with unit testing on java?

How to access a Static Class Private fields to unit test its methods using Microsoft Fakes in C#

how to pass a class to be tested to an unit test class

Test Config class with multiple private final fields

How can I unit test a method with multiple internal calls to class I want to mock using EasyMock

How to mock Application class to unit test ViewModel

How to define class instances per unit test?

How to unit test a class with private constructor?

How to unit test a class that implements Runnable

How to write a unit test for typed class?

How to mock an Akka Actor to Unit Test a class?

How do you moq a class for unit test

how to write unit test for android method and class?

How to unit test a class that uses context?

How is a unit test written for a method in this class?

FakeItEasy: how to unit test abstract class

How to Unit Test a project with Multiple Configurations

How to mock multiple components in camel unit test?

How to test generic abstract class with @Autowired fields?

How to access the fields of a test class with in an Rule in JUnit

how to map boolean fields in model class through hibernate annotations

How to test a void class method without arguments with Xcode Unit Test

How to write Unit Test for this class using Jersey 2 test framework

How to display multiple string values, based on boolean fields

How to Unit Test a class with a nested static class in java?

how to unit test a class extending an abstract class reading environment variables

How to unit test a class derived from a base class with lots of dependencies?