Testing libraries
JUnit: Unit testing
Mockito: Mock dependencies
Wiremock: Mock Servers, API response
MockMvc:
AssertJ: Assertion library (Junit also provide assertion)
TestNG:
@SpringBootTest
- Used to write integration tests
- load all beans
@Mock
@MockBean
import org.springframework.boot.test.mock.mockito.MockBean
@Spy
- keeps the methods as it is and modifies only the methods you want????
@WireMockTest
@MockMvc
Convert Mockito tests to SpringBootTest
AssertJ
- Has fluent style of assertion
assertThat(actual).isNull();
assertThat(notNullActual).isNotNull();
assertThat(actual).contains("great");
- Similar of Jest/Mocha/Chai which have fluent style of assertions
@ParametrizedTest
- can be used to pass different arguments instead of
@Test
@ParameterizedTest
@ValueSource(ints = { 0, 2, 4, 1000 })
void testIsEven(int arg) {
assertTrue(new Calculator().isEven(arg));
}
Lifecycle methods
@BeforeEach
- set up new instances of the classes being tested
@AfterEach
- clean up side effects
- provide execution info/report
@BeforeAll
- used on static method
- setup test fixture
- create and initialize big data structures
- establish connections to data sources
- fetch data from databases
@AfterAll
- used on static method
- tear down test fixture
- close resources and clean everything up
@BeforeAll and @AfterAll work on static methods so that they can be shared among new test class instances
- A test fixture is a fixed state of objects intended to provide a known and fixed environment for running tests.