Junior — Middle
Have you conducted automated testing of components interacting with a database?
sobes.tech AI
Answer from AI
Yes, automated testing of components interacting with a database typically includes:
- Unit tests using mocks or stubs to simulate database operations.
- Integration tests with a real or in-memory database (e.g., H2 for Java) to verify the correctness of queries and transactions.
Example of using H2 for testing DAO in Java:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
public void testFindByUsername() {
User user = new User("testuser", "password");
userRepository.save(user);
User found = userRepository.findByUsername("testuser");
assertNotNull(found);
assertEquals("testuser", found.getUsername());
}
}
This approach ensures that components work correctly with the database.