I've defined CompactDisc
bean in my spring application. According to Spring TestContext Framework , SpringJunit4ClassRunner
can make the following junit-jupiter
integration test aware of spring context.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CdConfig.class)
public class CdTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
But seems the CompactDisc
instance was not autowired.
> Task :soundsystem:test FAILED
CdTest > cdShouldNotBeNull() FAILED
org.opentest4j.AssertionFailedError at CdTest.java:35
In gradle.build
I use junit
platform to run the test,
test {
useJUnitPlatform()
testLogging {
events 'passed'
}
}
Did useJUnitPlatform()
overwrite the @RunWith(SpringJUnit4ClassRunner.class)
setting?
I tried with @ExtendWith(SpringExtension.class)
, now it works.
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = CdConfig.class)
public class CdTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
> Task :soundsystem:test
CdTest > cdShouldNotBeNull() PASSED
Anyone can explain why @RunWith(SpringJUnit4ClassRunner.class)
failed?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.