在一个简单的测试类中,如下所示:
@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)
在调试时,我实际上可以看到addressInfoLocalizer
对象是null
。
我用同样的方法创建了其他类,但只有这个类似乎有这个问题,可能是什么问题?
3条答案
按热度按时间zynd9foi1#
我发现并不总是@SpringBootTest就足够了,在某些类/情况下(老实说,我没有一个100%清晰的想法,所以我不会说愚蠢),它需要你手动选择测试运行程序,如下所示:
请记住,如果您决定自己示例化一个依赖项,那么Spring将无法注入所有需要的类,然后您需要将runner更改为类似
@RunWith(MockitoJUnitRunner.class)
的内容(JUNIT Autowired instance is always null的制作人员)
qaxu7uf22#
一种更好的方法是使用Spring的底层模拟bean。
这实际上是新推荐的方式。
kmbjn2e33#
这里有两件事:
1.如果您正在进行测试,理想的方法是mock而不是autowire(可能有例外)
1.您应该使用
@ExtendWith(MockitoExtension.class)
或@RunWith(MockitoJUnitRunner.class)
,以便在spring上下文中加载您的组件。但是,ExtendWith
是初始化bean/组件的首选方法。因为稍后的(RunWith)加载整个spring上下文可能需要更多时间因此,您的代码应该类似于:
参考/阅读更多:
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