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

应用程序失败时Spring MVC 测试通过

东方镜
2023-03-14

试图从第四版开始自学。Spring在行动。我正在构建一个web应用程序,使用书上的例子作为指南,并完成第5章。我使用Spring MVC有两个控制器和两个相应的测试。两个测试都通过了,但是当我在服务器(Tomcat 7.0)上运行应用程序时,它给出了一个基于ClinicalNoteRepository接口的UnsatisfiedDependencyException。当应用程序找不到ClinicalNoteControllerTest时,为什么这些测试——特别是ClinicalNoteControllerTest——会通过?

以下是控制台输出的摘录(由于字符限制,我无法完整发布):

临床笔记控制器测试:

package com.kwalker.practicewellness;

import static org.hamcrest.Matchers.hasItems;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.servlet.view.InternalResourceView;

import com.kwalker.practicewellness.data.ClinicalNoteRepository;
import com.kwalker.practicewellness.domain.ClinicalNote;
import com.kwalker.practicewellness.web.ClinicalNoteController;

public class ClinicalNoteControllerTest {

@Test
public void shouldShowRecentClinicalNotes() throws Exception {
    List<ClinicalNote> expectedClinicalNotes = createClinicalNoteList(20);
    ClinicalNoteRepository mockNoteRepository = mock(ClinicalNoteRepository.class);
    when(mockNoteRepository.findClinicalNotes(Long.MAX_VALUE, 20)).thenReturn(expectedClinicalNotes);

    ClinicalNoteController noteController = new ClinicalNoteController(mockNoteRepository);
    MockMvc mockMvc = standaloneSetup(noteController).setSingleView(
            new InternalResourceView("/WEB-INF/views/clinicalNotes.jsp")).build();

    mockMvc.perform(get("/clinical-notes"))
        .andExpect(view().name("clinical-notes"))
        .andExpect(model().attributeExists("clinicalNoteList"))
        .andExpect(model().attribute("clinicalNoteList", hasItems(expectedClinicalNotes.toArray())));
}

@Test
public void shouldShowPagedClinicalNotes() throws Exception {
    List<ClinicalNote> expectedClinicalNotes = createClinicalNoteList(50);
    ClinicalNoteRepository mockRepository = mock(ClinicalNoteRepository.class);
    when(mockRepository.findClinicalNotes(238900, 50)).thenReturn(expectedClinicalNotes);

    ClinicalNoteController noteController = new ClinicalNoteController(mockRepository);
    MockMvc mockMvc = standaloneSetup(noteController).setSingleView(
            new InternalResourceView("/WEB-INF/views/clinical-notes.jsp")).build();

    mockMvc.perform(get("/clinical-notes?max=238900&count=50"))
        .andExpect(view().name("clinical-notes"))
        .andExpect(model().attributeExists("clinicalNoteList"))
        .andExpect(model().attribute("clinicalNoteList", hasItems(expectedClinicalNotes.toArray())));
}

private List<ClinicalNote> createClinicalNoteList(int count) {
    List<ClinicalNote> clinicalNotes = new ArrayList<ClinicalNote>();
    for (int i=0; i < count; i++) {
        clinicalNotes.add(new ClinicalNote("Note " + i, new Date()));
    }
    return clinicalNotes;
}

}

CLINICalNoteController类:

package com.kwalker.practicewellness.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.kwalker.practicewellness.data.ClinicalNoteRepository;
import com.kwalker.practicewellness.domain.ClinicalNote;

@Controller
@RequestMapping("/clinical-notes")
public class ClinicalNoteController {

private static final String MAX_LONG_AS_STRING = "9223372036854775807";
private ClinicalNoteRepository noteRepository;

@Autowired
public ClinicalNoteController(ClinicalNoteRepository noteRepository) {
    this.noteRepository = noteRepository;
}

@RequestMapping(method=RequestMethod.GET)
public List<ClinicalNote> clinicalNotes(
        @RequestParam(value="max", defaultValue=MAX_LONG_AS_STRING) long max,
        @RequestParam(value="count", defaultValue="20") int count) {
    return noteRepository.findClinicalNotes(max, count);
}

}

以下是临床笔记存储库界面:

package com.kwalker.practicewellness.data;

import java.util.List;

import com.kwalker.practicewellness.domain.ClinicalNote;

public interface ClinicalNoteRepository {
    List<ClinicalNote> findClinicalNotes(long maximumId, int count);
}

POM文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kwalker</groupId>
<artifactId>practicewellness</artifactId>
<name>Practice Wellness</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>

<properties>
    <java-version>1.7</java-version>
    <org.springframework-version>4.1.4.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.6</org.slf4j-version>

</properties>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>   

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.10.19</version>
        <exclusions>
            <exclusion>
                <artifactId>hamcrest-core</artifactId>
                <groupId>org.hamcrest</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <exclusions>
            <exclusion>
                <artifactId>hamcrest-core</artifactId>
                <groupId>org.hamcrest</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
    </dependency>

    <!-- Misc -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalProjectnatures>
                      <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

共有1个答案

时旭东
2023-03-14

从这两行代码中可以看出:

ClinicalNoteRepository mockNoteRepository = mock(ClinicalNoteRepository.class);
ClinicalNoteController noteController = new ClinicalNoteController(mockNoteRepository);

...您的测试是单元测试,它们自己创建模拟存储库实例并使用这些模拟对象构造控制器。

在服务器上运行时,Spring配置用于确定框架需要管理哪些bean。因此,在您的情况下,您的配置可能存在问题。

在您提供的代码中,< code > clinical note repository 只是一个接口。必须有一个可以用作Spring Bean的接口实现。您没有向我们展示过这样的实现,所以可能只是因为您还没有编写这样的实现。这个规则的例外是如果您使用Spring Data,它提供了自己的实现。然而,在这种情况下,您的接口需要扩展< code>JpaRepository或其他Spring数据存储库接口之一。

请注意,您可以编写将导致加载Spring上下文的测试(从而测试您的配置)。为此,您需要将以下注释添加到测试类中:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyAppConfiguration.class)

...假设你用的是Spring Boot。或者像这样的东西

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyConfiguration.class }, 
                   loader = AnnotationConfigContextLoader.class)

…如果您有一个更传统的Spring应用程序,那么您可以将它指向一个或多个Java@Configuration类(如上所述)或XML配置文件列表。

 类似资料:
  • 我使用Live Code应用程序设计工具包创建了一个应用程序,我想在Android设备模拟器上使用Live Code中的测试按钮测试它。最后,我想在我的Nexus7上运行它。我遵循了“如何成为PC上的Android开发人员?”中的所有说明。Live Code提供的课程/教程。在Java SDK和Android SDK安装上,一切似乎都已经工作了,我配置了一个虚拟设备并启动了它,我为Android支

  • 我的JUnit测试用例失败了。行出现错误 mockito.When(BlogEntryService.Find(1L)).ThenReturn(entry); 故障跟踪是 java.lang.NullPointerException位于com.sample.controller.BlogentryControllerTest.GetExistingBlogEntry(BlogentryContro

  • 我想通过RobotFramework测试桌面电子应用程序。我下载了Selenium2Library,在网上看到了一些关于这方面的文章。我使用以下文本为变量创建vars.py:

  • 我运行< code>heroku open时出现应用程序错误。我查了日志,这是: 2016-06-19T05:22:44.640391 00:00 heroku[路由器]:at=错误代码=H10 desc=“应用程序崩溃”方法=获取路径=“/”主机=drawparty-.herokuapp。com request_id=6712804b-95f9-49ce-92a5-7f45df7bb79e fw

  • 我在Spring Boot应用程序中添加了一个测试用例。但是,当我

  • 我有一些mocha/chai/chai-http测试,它们遵循下面的结构,但是,每当一个测试失败时,我会得到一个 ,我似乎不知道它的来源。 UnhandledPromiserEjectionWarning:未处理的承诺拒绝。此错误可能是由于不带catch块的异步函数内部引发的,或者是由于拒绝了未用。catch()处理的承诺。 我尝试在 后面添加一个 承诺.reject(err)/code>,但也不