Testing with JUnit Flashcards
What happens if an assertions fails
A single unit test may have one to many assertions - assertions aren’t required for a unit test to be valid
If an assertion fails, the method will stop running
Should production code be run when running tests
No, the tests should call the required methods automatically and the actual program shouldn’t be run
What should the name of the test be for the method createAccount
testCreateAccount
=> This method will call the createAccount method
You can have multiple test methods per method, so long as it begins with testCreateAccount
How do you test your tests
Prove tests by introducing bugs and ensure that the tests catch the bugs
What are the 5 properties of good tests
Automatic - tests should run automatically
Thorough - All possible cases of code
Repeatable - Should produce the same result for a given input (tolerance of what is acceptable as “the same” should be introduced)
Independent - No tests should rely on each other or the environment that it’s in
Professional - Written and maintained to the same standard as the shipped code
We can use Right-BICEP to help construct tests. What does this stand for
Right => Boundary results are correct?
B => Boundary conditions
I => Inverse relationships (e.g. in maths you can use addition to check subtraction and use subtraction to check addition)
C => Can you cross-check results using other means
E => Error condition (e.g. if exceptions are caught)
P => Performance characteristics within bounds?
What do we need to take into consideration before we test methods
Any dependencies;
When testing methods, they may have interactions with other methods of the object or its attributes. These instances should be set up before tests for this specific method is tested
Give 3 reasons why unit tests are important
Reduces time spent on debugging
Communicates code’s intended use - illustrates how you want code to perform on various inputs and conditions
Code shouldn’t drift away from the limits put on them by the tests
What do the following annotations represent
@Before @BeforeClass @After @AfterClass @Test
1) Executing statements before a specific test case
2) Execute statements before all test cases
3) Execute statements after each test case (e.g. resetting variables)
4) Execute statements after all test cases (e.g. releasing resources after execution of all test cases)
5) Declares that it’s a test method
This is the main test method and most tests should be written under this method
What cant AssertEquals be used on
Why is it unwise to use AssertEquals on floating points
What is the purpose of the string parameter in the following code
assertEquals (String msg, expected, actual)
Objects and primitive types
There is only finite memory thus floating point values aren’t exact; we would need to specify some tolerance
This is the message that will be displayed if the test fails
THEORY CARD
Assertions aren’t essential when writing unit tests
Assertions enables automatic unit testing
If an assertion fails, the method will stop running altogether