mocking and coverage Flashcards

(17 cards)

1
Q

what is mocking

A

creating simulated objects that mimic behaviour of real objects in controlled ways

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

purpose of mocking

A

1) break dependencies
2) to simulate behaviour
3) isolate components

ALL during unit testing

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

why do we mock

VERY IF NOT MOST IMPORTANT FLASHCARD EVERY SENTENCE IS IMPORTANT HERE

A

We mock because we want to break external dependencies (keeping test loosely coupled) whilst also allowing the unit test to remain as a safety net that provides quick feedback. This is because the external dependency may involve a database and need long data base accesses or someother thing that takes time to excute like io and the unit test would become slow. Also when we have units that depend on other units it may introduce sideefects ( ie in the case of database where a real access may change the state of database which we dont want to do in a test)

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

When to mock

A

1) break dependencies between layers and tiers of code

explanation of 1)
break dependencies - i can mock an object rather than being forced to instantiate an object of it which means high coupling ( makes unit tests quick especially in cases where external dependy is database, network…)

2) corner cases and exceptional behaviour

explanation of 2)
logic may depend on exceptional stuff like security or something so i can mock the behaviour and feed it predetermined results so i can test that the actual logic works without being too woried about exceptional stuff and corner cases

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

when to not mock

A

Replacing awkward 3rd party apis
responsibilities which involve large amounts of state or data - which could be more conveniently expressed in a native format

HERE YOU MAKE A JUDGEMENT CALL IF MOCKING IS MORE EFFORT THAN JUST HAVING EXTERNAL DEPENDENCY DO NOT MOCK

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

law of demeter

A

methods should only interact with closely related objects to reduce coupling

good tip ( instead of classes talking to each other use an interface)

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

what is dependency injection

A

dependencies are injected into an object at runtime rather than created internally, improving testability and reducing coupling.

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

dummy

A

Placeholder objects that do nothing but avoid null values.

- Example: Empty object for mandatory parameters.

ie when we created an empty librarymember that does nothing when testing issueto which required a mandatory library member

tip: presence needed but they do nothing

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

stub

A

so stub - test double that delivers indirect inputs and is only used in test scope

example: public class AlwaysFailingDispenserStub implements IDispenser { public void dispense(BigDecimal amount) throws DispenserFailed { throw new DispenserFailed(ErrorType.HARDWARE, “Failing dispense”); } }

test doesnt directly produce the failure -> failure comes from dispense.dispense ( output of stub method)

STUB ONLY USED TO SIMULATE SPECIFIC SCENARIO ( which is why its only used in test scope)

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

fake

A

A fake is a mostly working implementation of a real class.
It either extends the real class or implements the same interface, but it hacks performance by overriding or replacing slow parts, such as database access, with simpler in-memory logic.

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

mock

A

mock - objects with expectations . We tell the mock what values we want to return. In test we run the real logic and see if it matches

can keep track of no of times method on mock object been called

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

spy

A

Similar to mock but tracks method invocations during testing.

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

Advantages of mocking

A
  1. Isolates Unit Under Test: Removes dependencies during testing.
  2. Facilitates Testing Edge Cases: Simulates unusual conditions.
  3. Improves Speed: Faster than real environment testing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

disadvantages of mocking

A
  1. Effort-Intensive: Setting up mock objects can be tedious.
  2. Limited Scope: Doesn’t test actual interactions with external systems.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

coverage

A

Measures the extent to which the codebase is executed during testing.

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

Read

A

int add(int a, int b) {
return a - b;
}
This function may achieve 100% test coverage (statement, branch, or path coverage), but it is incorrect because it does not meet the requirements of addition.

COVERAGE ONLY SHOWS CODE WAS EXECUTED - DOESNT ENSURE LOGIC IS CORRECT

17
Q

limitations of coverage

A
  • High coverage doesn’t guarantee correctness.
  • may miss
    • missing code
    • incorrect handling of boundary conditions
    • timing problems if program has timing dependencies
    • memory leaks for example in c++