Basics Flashcards

1
Q

Arrange, Act and Assert

A

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior. If the observed behavior is consistent with the expectations, the unit test passes, otherwise, it fails, indicating that there is a problem somewhere in the system under test.

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

Types of unit tests

A

Verifying that the system under test produces correct results, or that its resulting state is correct, is called state-based unit testing, while verifying that it properly invokes certain methods is called interaction-based unit testing.

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

True Unit Tests

A

Unit tests should fail only if there’s a bug in the system under test.
For example, tests may pass when running one-by-one, but fail when running the whole test suite, or pass on our development machine and fail on the continuous integration server. These situations are indicative of a design flaw. Good unit tests should be reproducible and independent from external factors such as the environment or running order.

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

@Test

A

@Test
void testGetAge() {
Person person = new Person(“Joey”, “Doe”, LocalDate.parse(“2013-01-12”));
long age = person.getAge();
assertEquals(4, age);
}

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

JUnit 5

A

Junit 5 is the latest version of the testing framework, and it has a lot of features when compared with Junit 4. JUnit 5, also known as JUnit Jupiter

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

@BeforeEach

A

Indicates that the annotated method should be executed before each test.

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

@AfterEach

A

Indicates that the annotated method should be executed after each test.

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

@BeforeAll

A

Indicates that the annotated method should be executed before all tests in the test class.

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

@AfterAll

A

Indicates that the annotated method should be executed after all tests in the test class.

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

@DisplayName

A

Provides a custom name for the test class or test method.

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

@Disabled

A

Disables the test method or class.

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

Parameterized Tests

A

This Parameterized Test is used to test a Test case with different parameters for this we use @ParameterizedTest annotations.

@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
void testSquare(int value) {
int result = square(value);
assertEquals(value * value, result, “Square calculation is incorrect”);
}

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

Test Lifecycle

A

https://media.geeksforgeeks.org/wp-content/uploads/20231228120919/JUnit-5-test-Lifecycle-660.png

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

Lifecycle Methods

A

A method annotated or meta-annotated with @BeforeAll, @AfterAll, @BeforeEach, or @AfterEach.

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

Test Class

A

A top-level class that contains at least one test method. It must not be abstract class.

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

Test Method

A

A method that is annotated or meta-annotated with @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, or @TestTemplate.

17
Q

JUnit 5 vs 4

A

Historically, JUnit 4 was monolithic and not designed to interact with popular build tools (Maven and Gradle) and IDEs (Eclipse, NetBeans, and IntelliJ). These tools had tight coupling with JUnit 4 and often relied on reflection to get the necessary information. This brought challenges, such as if the designers of JUnit decided to change the name of a private variable, this change could affect the tools that were accessing it reflectively.

18
Q

Junit 5

A

JUnit 5 is composed of several different modules from three different sub-projects:
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

19
Q

JUnit 5 Maven Dependencies

A

Version 5 of the JUnit is a modular one; you can no longer simply add a jar file to your project compilation classpath and your execution classpath

Jars required unit-jupiter-api, junit-jupiter-engine, junit-jupiter-params,junit-platform-suite,junit-vintage-engine

20
Q

Test Suites

A

Using JUnit 5 test suites, you can run tests spread into multiple test classes and different packages.

21
Q
A