【发布时间】:2020-12-08 21:36:39
【问题描述】:
我开始使用 Spring Boot 框架,每走一步都会遇到困难。 目前我有一个完全不明白的情况。
我正在使用 spring boot 2.3.5.RELEASE 构建与 maven
<properties>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.6.Final</version>
</dependency>
<!-- OpenAPi (swagger)-->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.32</version>
</dependency>
</dependencies>
我创建了一个相当简单的类 RESTCommunication。没有@Component、@Controller 或类似的。
public class RESTCommunication {
private final Logger m_logger = LoggerFactory.getLogger(this.getClass());
private String m_contentType;
private String m_path;
private String m_body;
private HashMap<String,String> m_urlParams;
private HashMap<String,String> m_queryParams;
public RESTCommunication() {
m_logger.debug("RESTCommunication constructor");
m_scheme = RESTCommunication.SCHEME_DEFAULT;
m_authority = RESTCommunication.AUTHORITY_DEFAULT;
m_port = RESTCommunication.PORT_DEFAULT;
m_path = RESTCommunication.PATH_DEFAULT;
m_urlParams = new HashMap<>();
// m_queryParams = new HashMap<>(); // I know that this is the reason for the exception
}
@Override
public String toString() {
StringBuilder sbToString = new StringBuilder();
sbToString.append(buildURI());
sbToString.append("|numURLParams:").append(m_urlParams.size());
sbToString.append("|numQueryParams:").append(m_queryParams.size()); // This has to fail because m_queryParams is not initialized
return sbToString.toString();
}
.....
对于这个类,我创建了一个非常简单的测试类 RESTCommunicationTest
@SpringBootTest
public class RESTCommunicationTest {
private final Logger m_logger = LoggerFactory.getLogger(this.getClass());
@Test
public void defaultInitTest() {
RESTCommunication restComm = new RESTCommunication();
String restCommURL = restComm.toString(); // Expect the nullpointer INSIDE the toString method
String expectedURL = sbExpected.toString();
assertEquals(expectedURL, restCommURL, "Expected URL does not match generated");
}
我使用以下命令运行测试:$./mvnw -e -X package -Dtest=RESTCommunicationTest#defaultInitTest
正如预期的那样,我在标准输出日志中看到了 Nullpointer。
但是在日志中我发现了这个:
webServerFactoryCustomizerBeanPostProcessor
websocketServletWebServerCustomizer
welcomePageHandlerMapping
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.532 s <<< FAILURE! - in org.highpots.hippo.core.communication.RESTCommunicationTest
[ERROR] defaultInitTest Time elapsed: 0.399 s <<< ERROR!
java.lang.NullPointerException
at org.ilovespringboot.not.RESTCommunicationTest.defaultInitTest(RESTCommunicationTest.java:36)
2020-12-08 22:18:10.810 INFO 10381 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] RESTCommunicationTest.defaultInitTest:36 » NullPointer
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
RESTCommunicationTest.java:36 中的空指针。绝对没有暗示真正的 NullPointer 发生在 RESTCommunication 类中。
有人可以解释我的这种行为吗?我做错了吗?
我知道我是某种编程恐龙。 (java项目在过去大约10年以上)。但我希望得到一个提示,抛出异常。我希望有一个由部分引起的详细堆栈跟踪。
如果无法想象这是不正常的行为。因为那意味着我必须通过完整的源代码来查找所有错误...这是不可能的
提前为您提供帮助 哈里·E
【问题讨论】:
标签: java spring-boot exception junit5