测试非阻塞的Spring MVC REST服务。

huangapple go评论84阅读模式
英文:

Testing non-blocking REST services with Spring MVC

问题

我有一个Spring MVC应用程序。
我想测试这个控制器:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:backoffice-servlet.xml")
public class TimeControllerTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }
    
    @Test
    public void should_OK() throws Exception {

        mockMvc.perform(get("/time/2")
                .contentType(APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}

这是我的 backoffice-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.bonanza.*" />
    <mvc:annotation-driven />
   
</beans>

但是当我启动应用程序时,我得到了这个错误:

Error creating bean with name 'documentController': Unsatisfied dependency expressed through field 'documentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentService': Unsatisfied dependency expressed through field 'documentRepo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bonanza.repositories.documents.DocumentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

仓库:

@Repository
public interface DocumentRepository extends JpaRepository<Document, Long> {
...
}
英文:

I have a Spring MVC application .
I want to test this controller:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(&quot;classpath:backoffice-servlet.xml&quot;)
public class TimeControllerTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }
    
    @Test
    public void should_OK() throws Exception {

        mockMvc.perform(get(&quot;/time/2&quot;)
                .contentType(APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}

this is my backoffice-servlet.xml:

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
        xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
        xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
        xmlns:util=&quot;http://www.springframework.org/schema/util&quot;
        xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
        xsi:schemaLocation=&quot;
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    &lt;context:component-scan base-package=&quot;com.bonanza.*&quot; /&gt;
    &lt;mvc:annotation-driven /&gt;

   
&lt;/beans&gt;

but when I start the app. I got this error:

Error creating bean with name &#39;documentController&#39;: Unsatisfied dependency expressed through field &#39;documentService&#39;; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name &#39;documentService&#39;: Unsatisfied dependency expressed through field &#39;documentRepo&#39;; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type &#39;com.bonanza.repositories.documents.DocumentRepository&#39; available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

the repo:

@Repository
public interface DocumentRepository extends JpaRepository&lt;Document, Long&gt; {
...
}

答案1

得分: 0

由于您要求只返回翻译好的部分,以下是您提供的内容的翻译:


作为 Spring Data 仓库,应在 XML 配置文件中添加适当的初始化:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    
    <context:component-scan base-package="com.bonanza.*" />
    <mvc:annotation-driven />
    <jpa:repositories base-package="<<REPOSITORY_PACKAGE_NAME>>" />
       
</beans>

这里是 Spring Data 参考指南 的链接。

如果没有使用 Spring Boot,还可能需要添加基础的 JPA 类 - dataSource、transactionManager、entityManagerFactory,并引用实体包。配置取决于测试环境:是使用真实的数据库/内存数据库还是其他选项。对于事务管理,XML 配置中还需要添加 <tx:annotation-driven />。这里是 Spring 参考指南 中适当章节的链接。

这里 可以找到实体管理器工厂/事务管理器 XML 配置的示例。

另一个示例来自这里

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    
    <context:property-placeholder location="/WEB-INF/spring/jdbc.properties" />
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
        p:username="${jdbc.username}" p:password="${jdbc.password}" />
    
    <bean id="hibernateJpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    
    <!-- 配置实体管理器工厂 bean -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <!-- 设置 JPA 属性 -->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.demo.data" />
    </bean>
    
    <!-- 配置事务管理器 bean -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    
    <tx:annotation-driven />
    
    <jpa:repositories base-package="com.demo.data" />
    <context:component-scan base-package="com.demo.svc" />
</beans>
英文:

As the repository is Spring Data repository, there should be added appropriate initialization in XML config file:

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:util=&quot;http://www.springframework.org/schema/util&quot;
xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
xmlns:jpa=&quot;http://www.springframework.org/schema/data/jpa&quot;
xsi:schemaLocation=&quot;
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa 
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
&lt;context:component-scan base-package=&quot;com.bonanza.*&quot; /&gt;
&lt;mvc:annotation-driven /&gt;
&lt;jpa:repositories base-package=&quot;&lt;&lt;REPOSITORY_PACKAGE_NAME&gt;&gt;&quot;/&gt;
&lt;/beans&gt;

Here is the link to Spring Data reference guide.

If Spring Boot is not used, also it may be needed to add infrastructure JPA classes - dataSource, transactionManager, entityManagerFactory with the reference to entities package. Configuration depends on test environment: is used real / in-memory database or other options. For transaction management there will be also &lt;tx:annotation-driven /&gt; needed in XML config. Here is link to appropriate chapter of Spring reference guide.

Here can be found an example of entity manager factory / transaction manager XML configuration.

Another example taken from here:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:p=&quot;http://www.springframework.org/schema/p&quot;
xmlns:context=&quot;http://www.springframework.org/schema/context&quot; xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
xmlns:jpa=&quot;http://www.springframework.org/schema/data/jpa&quot;
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd&quot;&gt;
&lt;context:property-placeholder location=&quot;/WEB-INF/spring/jdbc.properties&quot; /&gt;
&lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;
p:driverClassName=&quot;${jdbc.driverClassName}&quot; p:url=&quot;${jdbc.url}&quot;
p:username=&quot;${jdbc.username}&quot; p:password=&quot;${jdbc.password}&quot; /&gt;
&lt;bean id=&quot;hibernateJpaVendorAdapter&quot;
class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot; /&gt;
&lt;!-- Configure the entity manager factory bean --&gt;
&lt;bean id=&quot;entityManagerFactory&quot;
class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;
&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;
&lt;property name=&quot;jpaVendorAdapter&quot; ref=&quot;hibernateJpaVendorAdapter&quot; /&gt;
&lt;!-- Set JPA properties --&gt;
&lt;property name=&quot;jpaProperties&quot;&gt;
&lt;props&gt;
&lt;prop key=&quot;hibernate.dialect&quot;&gt;${hibernate.dialect}&lt;/prop&gt;
&lt;/props&gt;
&lt;/property&gt;
&lt;property name=&quot;packagesToScan&quot; value=&quot;com.demo.data&quot; /&gt;
&lt;/bean&gt;
&lt;!-- Configure the transaction manager bean --&gt;
&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;
&lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot; /&gt;
&lt;/bean&gt;
&lt;tx:annotation-driven /&gt;
&lt;jpa:repositories base-package=&quot;com.demo.data&quot; /&gt;
&lt;context:component-scan base-package=&quot;com.demo.svc&quot; /&gt;
&lt;/beans&gt;

huangapple
  • 本文由 发表于 2020年10月2日 04:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64162760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定