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

Spring MVC test passing when application is failing

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

以下是控制台输出的摘录(字符限制不允许我 post 整个事情):

Jan 12, 2015 5:30:23 AM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Practice Wellness' did not find a matching property. Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat/7.0.57 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server built: Nov 3 2014 08:39:16 UTC Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server number: 7.0.57.0 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name: Windows 7 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version: 6.1 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture: amd64 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JAVA_HOME: C:\Program Files\Java\jdk1.7.0_71\jre Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version: 1.7.0_71-b14 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor: Oracle Corporation Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE: C:\Users\kyle\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME: C:\Apache-Tomcat\apache-tomcat-7.0.57 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=C:\Users\kyle\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=C:\Apache-Tomcat\apache-tomcat-7.0.57 Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dwtp.deploy=C:\Users\kyle\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=C:\Apache-Tomcat\apache-tomcat-7.0.57\endorsed Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dfile.encoding=Cp1252 Jan 12, 2015 5:30:23 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0_71\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\HP SimplePass 2011\x64;C:\Program Files (x86)\HP SimplePass 2011\;;C:\Program Files\Broadcom\Broadcom 802.11\Driver;;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared.0\DLLShared\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files\Broadcom\WHL\;C:\Program Files\Broadcom\WHL\syswow64;C:\Program Files\Broadcom\WHL\SysWow64\;C:\Program Files\Broadcom\WHL\SysWow64\syswow64;C:\Program Files (x86)\Intel\Services\IPT\;C:\Program Files\Java\jdk1.8.0_25\bin;C:\Program Files\Gradle\Gradle-2.0\gradle-2.2.1\bin;. Jan 12, 2015 5:30:23 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] Jan 12, 2015 5:30:23 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] Jan 12, 2015 5:30:23 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 992 ms Jan 12, 2015 5:30:23 AM org.apache.catalina.core.StandardService startInternal INFO: Starting service Catalina Jan 12, 2015 5:30:23 AM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.57 Jan 12, 2015 5:30:26 AM org.apache.catalina.core.ApplicationContext log INFO: Spring WebApplicationInitializers detected on classpath: [com.kwalker.practicewellness.config.WellnessWebAppInitializer@2b1ecc13] Jan 12, 2015 5:30:26 AM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring root WebApplicationContext INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Mon Jan 12 05:30:26 MST 2015]; root of context hierarchy INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.kwalker.practicewellness.config.RootConfig] INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring WARN : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clinicalNoteController' defined in file [C:\Users\kyle\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Practice Wellness\WEB-INF\classes\com\kwalker\practicewellness\web\ClinicalNoteController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.kwalker.practicewellness.data.ClinicalNoteRepository]: : No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} ... continued... Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} ... continued... ... 24 more ERROR: org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clinicalNoteController' defined in file [C:\Users\kyle\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Practice Wellness\WEB-INF\classes\com\kwalker\practicewellness\web\ClinicalNoteController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.kwalker.practicewellness.data.ClinicalNoteRepository]: : No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1131) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4992) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ... 24 more Jan 12, 2015 5:30:27 AM org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clinicalNoteController' defined in file [C:\Users\kyle\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Practice Wellness\WEB-INF\classes\com\kwalker\practicewellness\web\ClinicalNoteController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.kwalker.practicewellness.data.ClinicalNoteRepository]: : No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1131) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4992) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kwalker.practicewellness.data.ClinicalNoteRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionExcepti on(DefaultListableBeanFactory.java:1308) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ... 24 more Jan 12, 2015 5:30:27 AM org.apache.catalina.core.StandardContext startInternal SEVERE: Error listenerStart Jan 12, 2015 5:30:27 AM org.apache.catalina.core.StandardContext startInternal SEVERE: Context [/practicewellness] startup failed due to previous errors Jan 12, 2015 5:30:27 AM org.apache.catalina.core.ApplicationContext log INFO: Closing Spring root WebApplicationContext Jan 12, 2015 5:30:27 AM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] Jan 12, 2015 5:30:27 AM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["ajp-bio-8009"] Jan 12, 2015 5:30:27 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 3494 ms

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 class:

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);
}

}

这是 ClinicalNoteRepository 界面:

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>

从这两行代码可以看出:

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

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

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

在您提供的代码中,ClinicalNoteRepository只是一个接口。必须有一个可用作 Spring Bean 的接口的实现。您还没有向我们展示这样的实现,所以可能只是您还没有编写一个。此规则的例外情况是您使用 Spring 数据,它提供自己的实现。但是,在这种情况下,您的接口需要扩展 JpaRepository 或其他 Spring 数据存储库接口之一。

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

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

...假设您正在使用 Spring Boot。或者诸如

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

...如果您有一个更传统的 Spring 应用程序,在这种情况下,您会将其指向一个或多个 Java @Configuration classes(如以上)或 XML 配置文件列表。