ExampleRepository
injected by @Autowired
annotation into test class works well, but injected into service class is null. Why does it happen and how can I fix it?
Repository class
public interface ExampleRepository extends JpaRepository<Example, Long> {
// ...
}
Service class
@Service
public class Feed {
@Autowired
private ExampleRepository exampleRepository;
private File file;
public Feed(String file) {
this.file = new File(file);
}
// ...
}
Test class
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public abstract class ExampleTests {
@Autowired
private ExampleRepository exampleRepository;
@BeforeEach
public void setUp() {
restoreInitialData();
}
protected void restoreInitialData() {
this.exampleRepository.deleteAll();
}
@Test
public void test() {
Feed feed = new Feed("example.json");
Optional<Example> example = feed.ingest();
assertTrue(example.isPresent());
}
// ...
}