Testing with JUnit Flashcards

1
Q

What happens if an assertions fails

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Should production code be run when running tests

A

No, the tests should call the required methods automatically and the actual program shouldn’t be run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What should the name of the test be for the method createAccount

A

testCreateAccount
=> This method will call the createAccount method

You can have multiple test methods per method, so long as it begins with testCreateAccount

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you test your tests

A

Prove tests by introducing bugs and ensure that the tests catch the bugs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 5 properties of good tests

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

We can use Right-BICEP to help construct tests. What does this stand for

A

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?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do we need to take into consideration before we test methods

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give 3 reasons why unit tests are important

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What do the following annotations represent

@Before
@BeforeClass
@After
@AfterClass
@Test
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

THEORY CARD

Assertions aren’t essential when writing unit tests

Assertions enables automatic unit testing

A

If an assertion fails, the method will stop running altogether

How well did you know this?
1
Not at all
2
3
4
5
Perfectly