当前位置: 首页 > 知识库问答 >
问题:

Spring测试log4j错误

胡致远
2023-03-14
package com.projects.myproject.tests.controllers;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(MockitoJUnitRunner.class)
public class LegislaturaControllerTest {

    private MockMvc mockMvc;

    @Test
    public void testList() throws Exception {   
        mockMvc.perform(get("/clientes/list"));
    }
}
<dependencies>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.14.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j13</artifactId>
    <version>1.0.1</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.13.RELEASE</version>
</dependency>
<dependency>
    <groupId>es.juntaex.asambleadata</groupId>
    <artifactId>asamblea-data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.10.19</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.14.RELEASE</version>
</dependency>

共有1个答案

袁阿苏
2023-03-14

在您的问题中,mockMvc对象未初始化。在字段顶部添加@mock注释。

@Mock
MockMvc mockMvc;

通过添加,

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}

之后,如果您在运行测试方法之前检查所有对象是否存在,将会非常好,方法是添加

@Test
public void testMock() {
    assertNotNull(mockMvc);
}
 类似资料:
  • 本节介绍与log4j Framework相关的各种模拟测试。 您可以在本地计算机上下载这些示例模拟测试,并在方便时离线解决。 每个模拟测试都提供一个模拟测试密钥,让您自己验证最终得分和评分。 .B, .answer-sheet{ display:none !important; } .Q a{ pointer-events: none; cursor: default; } log4j Mock

  • 以下测验提供了与log4j Framework相关的多项选择题(MCQ)。 您必须阅读所有给定的答案并单击正确的答案。 如果您不确定答案,则可以使用“ Show Answer按钮Show Answer 。 您可以使用“ Next Quiz按钮在Next Quiz中检查新的问题集。 问题1 - 关于log4j,以下哪项是正确的? A - 它支持每个记录器的多个输出appender。 B - 它针对速

  • 我写了一个jUnit测试,但我在执行它时遇到了错误: 我的应用程序是这样配置的: 我的测试: 我的上下文配置: 我的网络。xml配置: 根据网络上的不同教程,配置似乎很好,但我不知道错误来自哪里。 我认为TestDao是一个上下文。我的测试类没有很好地实现xml,因为当我将上下文文件移动到另一个项目时,错误根本不会改变。此外,我测试了许多不同的方法来配置@ContextConfiguration,

  • 我已经使用Spring云流启动了一个小型微服务。 我只有两个流绑定,如下所示: 我用Serenity开发了组件测试,我将通道注入到我想要发送测试消息的地方: 哪里: 只是定义为字符串常量: 组件测试模块导入依赖项: 我发送的信息如下: 快乐流工作正常。但是,我想在侦听器无法处理消息时测试错误流。 这是一个监听器的例子: 从try/catch引发异常时,错误由服务激活器处理: 在没有Spring-C

  • 在程序运行过程中,总会遇到各种各样的错误。 有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修复的。 有的错误是用户输入造成的,比如让用户输入email地址,结果得到一个空字符串,这种错误可以通过检查用户输入来做相应的处理。 还有一类错误是完全无法在程序运行过程中预测的,比如写入文件的时候,磁盘满了,写不进去了,或者从网络抓取数据,网

  • 在程序运行过程中,总会遇到各种各样的错误。 有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修复的。 有的错误是用户输入造成的,比如让用户输入email地址,结果得到一个空字符串,这种错误可以通过检查用户输入来做相应的处理。 还有一类错误是完全无法在程序运行过程中预测的,比如写入文件的时候,磁盘满了,写不进去了,或者从网络抓取数据,网