java异常(stacktrace)在springboot单元测试中不可用

q3aa0525  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(300)

我开始使用spring-boot框架,每走一步都会遇到困难。目前我有一个情况,我绝对不明白。
我正在使用springboot2.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>

我创建了一个相当简单的类。无 @Component ,@控制器或类似产品。

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 正如预期的那样,我在stdout日志中看到了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多年的时间)。但是我希望得到一个异常抛出的提示。我希望有一个stacktrace和详细的部分引起的。
如果不能想象这是不正常的行为。因为那就意味着我必须通过仔细查看完整的源代码来找出所有的错误。。。这是不可能的
谢谢你的帮助

dpiehjr4

dpiehjr41#

maven使用surefire插件运行测试。要查看异常stacktrace,必须在surefire插件配置中将trimstacktrace设置为false。
历史上surefire插件用于打印完整的堆栈跟踪。虽然堆栈跟踪中的信息对调试很有用,但堆栈跟踪在控制台输出中占用了相当多的空间。特别是如果有很多测试失败,那么很容易在噪音中迷失方向。
由于版本2.13,surefire默认只打印摘要。

<build>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${your.maven.surefire.version.here}</version>
            <configuration>
                <trimStackTrace>false</trimStackTrace>
            </configuration>
        </plugin>
      </plugins>
    </build>

相关问题