Unit Testing and TDD Flashcards

1
Q

Unit Testing

A

Testing units in a system, in OOP this means individual classes and methods. Should account for most of the testing in a project and done by developers.

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

JUNIT

A

testing framework for java using Maven.

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

Anatomy of a Unit Test

A

Setup
Exercise
Verify
Teardown

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

Before and After Methods

A
  • Setup and Teardown actions can be repetitive across tests.
  • JUNIT provides easy ways of defining these
  • Methods tagged with @BeforeEach will run before each test
  • Same way with @AfterEach
  • BeforeAll and AfterAll are the same
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What makes a good unit test

A
  • Descriptive name
  • Tests one thing
  • Always returns the same result
  • Has no conditional logic
  • Independent of other tests
  • So understandable that it acts as documentation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What makes a bad unit test

A
  • Interacts with external databases
  • Communicates across a network
  • Interacts with a file system
  • Must be executed after specific tests
  • Requires configuration of the environment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Code Coverage

A

Measure used to describe how much of the source code has been executed when the test suite runs. The goal is at least 80%.

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

Does 100% code coverage mean no bugs?

A

No, there might be unreachable code

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

Test Driven Development

A

Write enough code to pass the test

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

Benefits of TDD

A
  • Driven by clear goals
  • Safer Code
  • Isolation of incorrect code
  • Easily introduce new functionality
  • Document the application as you go
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Testing Doubles

A

Dealing with indirect inputs and outputs, an object in place of a real component for the purpose of running a test, includes; dummies, spies, stubs and mocks.

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

Dummy Objects

A

Degenerate form of test double, not used by the DOC or used in a limited manner. Usually an alternative to using null objects. Only exist to be passed around

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

Test Stubs

A

Replace a real component on which SUT depends. Delivers a pre-canned response and produces indirect input to force the execution of specific code paths.

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

Test Spies

A

More advanced form of stub, besides producing indirect inputs, spies can monitor and record indirect outputs. Test driver can verify with a spy that something has been produced.

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

Mock Objects

A

Powerful, do everything a stub or spy does but also verifies SUT behavior. Test driver tells the mock what sort of behavior is expected during the test, and the mock will verify that the behavior was observed. Using mocks requires proper layering of your system.

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

Setting up a Mock with Mockito

A
  1. Define TimeProvider Interface
  2. Write your SUT code
  3. Write your tests using mocks

You don’t need to create an interface for every item you want to mock, especially if the class you want to mock is not implemented.

17
Q

Test Patterns

A

A design pattern is similar to a recipe where a solution to a common design problem is explained. Test patterns are the same way but in testing. Directly influence the system design.

18
Q

Dependency Injection

A

Avoid hardcoding dependencies into the system, find a way to allow depended upon components to be set at runtime (Parameter, Constructor, Setter Injection)

19
Q

Dependency Lookup

A

Design system in order to look up dependencies at runtime when their need arises. Usually a factory approach is taken. Tests can gain control of the factory to then control dependencies for testing purposes.