With Junit 5 you can do this (@BeforeAll instead of @BeforeClass)
public void ITest {
@Autowired
private EntityRepository dao;
@BeforeAll
public static void init(@Autowired EntityRepository dao) {
dao.save(initialEntity); //possible now as autowired function parameter is used
}
}
By leaving the field it means it can be used in other tests
Related Contents:
- Rollback transaction after @Test
- Spring Boot properties in ‘application.yml’ not loading from JUnit Test
- How to run JUnit SpringJUnit4ClassRunner with Parametrized?
- Populating Spring @Value during Unit Test
- Difference between @Mock, @MockBean and Mockito.mock()
- Unable to find a @SpringBootConfiguration when doing a JpaTest
- How to set environment variable or system property in spring tests?
- JUnit tests pass in Eclipse but fail in Maven Surefire
- Overriding beans in Integration tests
- What is the difference between SpringJUnit4ClassRunner and SpringRunner
- Injecting @Autowired private field during testing
- How to re-create database before each test in Spring?
- How to exclude *AutoConfiguration classes in Spring Boot JUnit tests?
- Spring JUnit: How to Mock autowired component in autowired component
- Difference between MockMvc and RestTemplate in integration tests
- Spring not autowiring in unit tests with JUnit
- Spring profiles and testing
- Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test
- How to check JSON in response body with mockMvc
- reason: no instance(s) of type variable(s) T exist so that void conforms to using mockito
- How to use @InjectMocks along with @Autowired annotation in Junit
- Spring boot 1.4 Testing : Configuration error: found multiple declarations of @BootstrapWith
- @Value “Could not resolve placeholder” in Spring Boot Test
- Set System Property With Spring Configuration File
- How to unit test a Spring MVC controller using @PathVariable?
- Maven not running Spring Boot tests
- JUnit-testing a Spring @Async void service method
- Does Spring @Transactional attribute work on a private method?
- How to manage exceptions thrown in filters in Spring?
- Difference between @Valid and @Validated in Spring
- Junit – run set up method once
- JUnit: how to avoid “no runnable methods” in test utils classes
- No tests found with test runner ‘JUnit 4’
- How to run all tests in a particular package with Maven?
- How to run bootRun with spring profile via gradle task
- How to specify prefix for all controllers in Spring Boot?
- Return generated pdf using spring MVC
- Self injection with Spring
- Create multiple parameter sets in one parameterized class (junit)
- Spring Rest POST Json RequestBody Content type not supported
- Spring Security Custom Authentication – AuthenticationProvider vs UserDetailsService
- Spring Scheduler stops unexpectedly
- Naming convention JUnit suffix or prefix Test [closed]
- Spring-Batch without persisting metadata to database?
- Spring Data Rest and Cors
- No matching bean of type … found for dependency
- How to find distinct rows with field in list using JPA and Spring?
- How can I wipe data from my HSQLDB after every test?
- Spring Boot, Spring Data JPA with multiple DataSources
- “Forked Java VM exited abnormally” error from junit tests