Testing Flashcards

1
Q

Types of testing?

A

Unit testing - functions/interfaces
System Testing -
Integration Testing

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

@DisplayName

A

annotation to change name of the test in the left bottom corner of the IDE

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

assertEquals

A

The assertEquals assertion verifies that the expected and the actual values are equal:

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

assertArrayEquals

A

If we want to assert that two arrays are equals, we can use the assertArrayEquals:
@Test
public void whenAssertingArraysEquality_thenEqual() {
char[] expected = {‘J’,’u’,’n’,’i’,’t’};
char[] actual = “Junit”.toCharArray();

assertArrayEquals(expected, actual); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

assertNotNull and assertNull

A

@Test
public void whenAssertingNull_thenTrue() {
Object car = null;

assertNull("The car should be null", car); } In the opposite way, if we want to assert that an object should not be null we can use the assertNotNull assertion.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

assertNotSame and assertSame

A

With assertNotSame, it’s possible to verify if two variables don’t refer to the same object:

@Test
public void whenAssertingNotSameObject_thenDifferent() {
    Object cat = new Object();
    Object dog = new Object();
assertNotSame(cat, dog); } Otherwise, when we want to verify that two variables refer to the same object, we can use the assertSame assertion.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

assertTrue and assertFalse

A

In case we want to verify that a certain condition is true or false, we can respectively use the assertTrue assertion or the assertFalse one:

@Test
public void whenAssertingConditions_thenVerified() {
assertTrue(“5 is greater then 4”, 5 > 4);
assertFalse(“5 is not greater then 6”, 5 > 6);
}

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

fail

A

The fail assertion fails a test throwing an AssertionFailedError. It can be used to verify that an actual exception is thrown or when we want to make a test failing during its development.

Let’s see how we can use it in the first scenario:

@Test
public void whenCheckingExceptionMessage_thenEqual() {
try {
methodThatShouldThrowException();
fail(“Exception not thrown”);
} catch (UnsupportedOperationException e) {
assertEquals(“Operation Not Supported”, e.getMessage());
}
}

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

assertThat

A

The assertThat assertion is the only one in JUnit 4 that has a reverse order of the parameters compared to the other assertions.

In this case, the assertion has an optional failure message, the actual value, and a Matcher object.
Let’s see how we can use this assertion to check if an array contains particular values:

@Test
public void testAssertThatHasItems() {
    assertThat(
      Arrays.asList("Java", "Kotlin", "Scala"), 
      hasItems("Java", "Kotlin"));
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

assertAll

A

checks for multiple methods in the same time

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

assertTimeout

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

Difference between assertion and assumption

A

Assumption
met- Test runs
not met- Test gets aborted

Assertion
met- Test Succeeds
not met- Test fails

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

@Nested

A

Will make the child class test get running when the we run the class test

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

@Inject

A

Annotation used for junit 5 for DI

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

@ExtendsWith

A

Will put it on top of the class in which we are going to perform our tests on and decorate it with

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

How to Di in JUNIT?

A

Extending the ParameterResolverMethod and instantiating it