Java 用SpringMVC测试非阻塞REST服务

Java 用SpringMVC测试非阻塞REST服务,java,spring,spring-mvc,junit,mockmvc,Java,Spring,Spring Mvc,Junit,Mockmvc,我有一个SpringMVC应用程序。 我要测试此控制器: @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("classpath:backoffice-servlet.xml") public class TimeControllerTests { @Autowired private WebApplicationContext wac;

我有一个SpringMVC应用程序。 我要测试此控制器:

@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());
    }
}
这是我的
backofficeservlet.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>
回购协议:

@Repository
public interface DocumentRepository extends JpaRepository<Document, Long> {
...
}
@存储库
公共接口文档存储库扩展了JpaRepository{
...
}

由于存储库是Spring数据存储库,因此应在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 Boot,则可能还需要添加基础结构JPA类—dataSource、transactionManager、entityManagerFactory,并引用entities包。配置取决于测试环境:使用实/内存数据库或其他选项。对于事务管理,XML配置中还需要
。这里是链接到适当的章节

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

另一个例子来自:


${hibernate.dial}

由于存储库是Spring数据存储库,因此应在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 Boot,则可能还需要添加基础结构JPA类—dataSource、transactionManager、entityManagerFactory,并引用entities包。配置取决于测试环境:使用实/内存数据库或其他选项。对于事务管理,XML配置中还需要
。这里是链接到适当的章节

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

另一个例子来自:


${hibernate.dial}

尝试将
更改为
正常结果,先生:-(您试过打印所有spring加载的bean吗?像这样,我想知道backoffice-servlet.xml是否没有加载。它是cp上的defo吗?它不是SpringBoot应用程序。您可能应该将
添加到xml配置中。尝试将
更改为
正常结果,先生:-(您试过打印所有spring加载的bean吗?像这样,我想知道backoffice-servlet.xml是否没有加载。它是cp上的defo吗?它不是SpringBoot应用程序。您可能应该将
添加到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" />

    <!-- Configure the entity manager factory bean -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <!-- Set JPA properties -->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.demo.data" />
    </bean>

    <!-- Configure the transaction manager 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>