- Unit testing
- Integration testing
How can you create a shared application context in a JUnit integration test?
- Enable Spring TestContext Framework to load shared application context, using below JUnit runner:
@RunWith(SpringRunner.class) @ContextConfiguration(classes = OrderConfiguration.class) public class OrderServiceTests implements ApplicationContextAware { }
- Variation of above:
@ContextConfiguration(classes = OrderConfiguration.class)
public class OrderServiceTests implements ApplicationContextAware extends AbstractJUnit4SpringContextTests { }
- Use @DirtiesContext to re-create context for next tests
When and where do you use @Transactional in testing?
- Method level:
- @Transactional - Method runs in own transaction rollbacked at the end by default
- To override above, use @TransactionConfiguration(defaultRollback = false) in class level
- Class level:
- @Transactional - All methods run in own transaction rollbacked at the end by default
- To override above, use @Rollback(false) or @Commit in desired method
How are mock frameworks such as Mockito or EasyMock used?
- Declare the mock:
OrderBook mockOrderBook = mock(OrderBook.class);
- Or using annotation @Mock:
- Use @RunWith(MockitoJUnitRunner.class) in class method
- Or MockitoAnnotations.initMocks(this) in before / setup method
- Or (Spring Boot), use @MockBean which mocks Spring beans
- Inject mock in related component
- Set behaviour when called:
when(mockOrderBook.findAllForBook()).thenReturn(books);
- Test and verify with Assertions
How is @ContextConfiguration used?
- In Spring, it's used for loading an ApplicationContext, along with a JUnit runner
- Example 1:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={BookConfig.class}) public class BookConfigTest {}
- Example 2:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class BookConfigTest
{}- Loads context from BookConfigText-context.xml classpath file
- In Spring Boot, @SpringBootTest includes @ContextConfiguration annotation
How does Spring Boot simplify writing tests?
- Adding dependency spring-boot-starter-test provides:
spring-boot-test, spring-boot-test-autoconfigure
which provide below:
JUnit: The de-facto standard for unit testing Java applications.
Spring Test & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
AssertJ: A fluent assertion library.
Hamcrest: A library of matcher objects (also known as constraints or predicates).
Mockito: A Java mocking framework.
JSONassert: An assertion library for JSON.
JsonPath: XPath for JSON.
- Instantiate dependencies by using new or mock, no need to involve Spring
- Integration testing with Spring ApplicationContext with or without requiring deployment (MockMvc)
- Spring Boot: Detects automatically test configuration without @ContextConfiguration and declaring @Configuration classes
Detects classes @SpringBootApplication or @SpringBootConfiguration
- Spring Boot: Use *Test annotations to introduce test slicing.
Example: @DataJpaTest, @WebMvcTest
- Spring’s test framework caches application contexts between tests.
the potentially time-consuming process of loading the context happens only once.
- @TestConfiguration: Can exclude loading automatically configuration classes belonging in src/test/java.
Then, can decide to @Import or not the specific configuration
What does @SpringBootTest do?
How does it interact with @SpringBootApplication and@SpringBootConfiguration?
- Used to load a full Application Context, either running a server or in mock environment
- Includes option to load specific properties
- SpringBootTest annotation traverses related class package upwards until a @SpringBootConfiguration is located
- Note that @SpringBootApplication included @SpringBootConfiguration
- It then constructs a whole Application Context
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
What may be missing, or could get better?