24-Testing Flashcards
From what Spring version JUnit 5 becomes the default JUnit version?
Spring Boot 2.2
Components in JUnit 5
Platform
Jupiter: extension model & extensions
Vintage: support JUnit 3, 4
What is the big difference between JUnit 4 and 5? Give an example
The big difference between 4 and 5 is the extension framework. Allows multiple frameworks to customize tests at the same time. For example to run Spring-based tests and use the test support of a mocking library too.
New annotations in JUnit 5
@DisplayName - Displays the name of each test on the console when it is run
@Nested - Multiple nested tests can be defined in a single test
@ParameterizedTest - Run the same test multiple times with different arguments
How to ignore a test in JUnit 5?
@Disabled
What is @ContextConfiguration used for?
configration for TestContext
Jar file of spring test framework
spring-test.jar
What framework defines application context for Spring tests?
TestContext framework
How to define a test class with SpringExtension?
@ExtendWith(SpringExtension.class) // mandatory @ContextConfiguration(classes={SystemTestConfig.class}) // mandatory public class TransferServiceTests {}
Note: shortcut @SpringJUnitConfig(SystemTestConfig.class)
In which env does the following definition works? void hello(@Autowired TransferService ts) {}
Only in test we can use @Autowired for argument
What happens if using @SpringJUnitConfig without configuration class?
Spring will search static @Configuration embedded in test class
About @TestPropertySource
Custom properties just for testing
Has higher precedence than sources
example:
@SpringJUnitConfig(SystemTestConfig.class)
@TestPropertySource(properties = { “username=foo”, “password=bar” },
locations = “classpath:/transfer-test.properties”)
Write a Spring test with JUnit4
@RunWith(SpringRunner.class) @ContextConfiguration(SystemTestConfig.class) public class TransferServiceTests {}
Note: can use either SpringRunner or SpringJUnit4ClassRunner