With SpringBoot + JUnit5
, do it until the constructor pattern injection is moved at the time of testing.
It worked if I gave @ExtendWith (SpringExtension.class)
and @SpringBootTest
.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@DisplayName("Test to move constructor pattern")
@ExtendWith(SpringExtension.class)
@SpringBootTest
class AutowiredTest {
private final AutowiredClass autowiredClass;
private final String hoge;
private final String fuga;
@Autowired
AutowiredTest(AutowiredClass autowiredClass) {
this.autowiredClass = autowiredClass;
hoge = "String";
fuga = "String";
}
@Test
@DisplayName("Some test")
void uploadFile() {
assertEquals(hoge, fuga);
}
}
Since it is JUnit5, you can write various things in the class without adding public
.
Also, the constructor will inject even private
.
Recommended Posts