Junit测试中的@Autowired Injected依赖项始终为null



在下面这样的简单测试类中:

@SpringBootTest
@Slf4j
@ActiveProfiles("test")
public class LocalizerTest {
@Autowired
AddressInfoLocalizer addressInfoLocalizer;

@Test
public void localizeIp() {
String ip = "8.8.8.8";
Optional<CityResponse> response = addressInfoLocalizer.localize(ip);
response.ifPresent(cityResponse -> log.info("{}", cityResponse));
}
}

AddressInfoLocalizer(很抱歉有个奇怪的名字,但我不得不编出来(基本上是这样的:


@Slf4j
@Component
public class AddressInfoLocalizer {
@Value("${geolocalization.dbpath}")
private String databasePath;
private DatabaseReader database;
@PostConstruct
public void initialize() {
log.info("GeoIP2 database path:{}", databasePath);
File database = new File(databasePath);
try {
this.database = new DatabaseReader.Builder(database).build();
} catch (IOException ioe) {
this.database = null;
log.warn("Problems encountered while initializing IP localization database, skipping resolutions");
}
}
public Optional<CityResponse> localize(String ipAddressString) {
if (isNull(database)) {
log.warn("Attempted to resolve an IP location with database problems, skipping.");
return Optional.empty();
}
try {
InetAddress ipAddress = InetAddress.getByName(ipAddressString);
return ofNullable(database.city(ipAddress));
} catch (UnknownHostException uhe) {
log.error("Unknown host {}, {}", ipAddressString, uhe.getCause());
return Optional.empty();
} catch (IOException ioe) {
log.error("IO error while opening database, {}", ioe.getCause().toString());
return Optional.empty();
} catch (GeoIp2Exception gip2e) {
log.error("GeoIP error, {}", gip2e.getCause().toString());
return Optional.empty();
}
}
}

在调用(测试中(addressInfoLocalizer.localize(ip);、时,我一直收到NullPointerException

java.lang.NullPointerException
at com.bok.parent.LocalizerTest.localizeIp(LocalizerTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

在调试时,我实际上可以看到CCD_ 2对象是CCD_。

我以同样的方式创建了其他类,但似乎只有这一个有这个问题,可能出了什么问题?

我发现@SpringBootTest并不总是足够的,在某些类/情况下(老实说,我没有100%清楚的想法,所以我不会说愚蠢(需要手动选择测试运行程序,如下所示:

@SpringBootTest
@RunWith(SpringRunner.class)
public class FooTest {
...
}

请记住,如果您决定自己实例化依赖项,那么Spring将无法注入所有需要的类,然后您需要将runner更改为类似@RunWith(MockitoJUnitRunner.class)的东西

(JUNIT Autowired实例的积分始终为空(

更好的方法是使用spring的底层mock bean。

@SpringBootTest(classes= AddressInfoLocalizer.class)

这实际上是推荐的新方法。

这里有两件事:

  1. 如果您正在测试,理想的方法是mock而不是autowire(可能会有例外(
  2. 您应该使用@ExtendWith(MockitoExtension.class)@RunWith(MockitoJUnitRunner.class),以便在spring上下文中加载组件。然而,ExtendWith是初始化bean/组件的首选方式。因为后面的(RunWith(加载整个spring上下文,这可能需要更多时间

因此,您的代码应该看起来像:

@Slf4j
@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public class LocalizerTest {
@Mock
AddressInfoLocalizer addressInfoLocalizer;

@Test
public void localizeIp() {
String ip = "8.8.8.8";
//Mock here.
Optional<CityResponse> response = addressInfoLocalizer.localize(ip);
response.ifPresent(cityResponse -> log.info("{}", cityResponse));
}
}

参考/阅读更多:
https://www.baeldung.com/mockito-junit-5-extension
https://stackoverflow.com/questions/55276555/when-to-use-runwith-and-when-extendwith
https://www.javadoc.io/static/org.mockito/mockito-junit-jupiter/3.10.0/org/mockito/junit/jupiter/MockitoExtension.html

相关内容

最新更新