小编ste*_*nes的帖子

无法在Spring MVC Controller测试中模拟Service类

我有一个Spring 3.2 MVC应用程序,并使用Spring MVC测试框架来测试我的控制器操作的GET和POST请求.我正在使用Mockito来模拟服务,但我发现模拟被忽略了,而且我的实际服务层正被使用(因此,数据库正在被击中).

我的Controller测试中的代码:

package name.hines.steven.medical_claims_tracker.controllers;

import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import name.hines.steven.medical_claims_tracker.domain.Policy;
import name.hines.steven.medical_claims_tracker.services.PolicyService;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({ "classpath:/applicationContext.xml", "classpath:/tests_persistence-applicationContext.xml" })
public class PolicyControllerTest {

    @Mock
    PolicyService service;

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() …
Run Code Online (Sandbox Code Playgroud)

java spring mocking spring-mvc mockito

41
推荐指数
4
解决办法
8万
查看次数

与Flyway/Spring和H2嵌入式数据库相关的模式问题

我正在构建一个使用MySQL数据库的Spring 3 MVC应用程序,并且最近将Flyway集成到解决方案中以管理数据库迁移.我已根据Flyway文档成功配置了我的applicationContext.xml,以便在应用程序启动时,Flyway将迁移到最新版本.

我无法让Flyway与我的单元/功能测试很好地配合使用.我使用Spring Data JPA作为我的数据访问层,并构建了一些JUnit测试来测试一些自定义查询.

我用于这些测试的应用程序配置是:

<jdbc:embedded-database id="dataSource" type="H2" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
  <property name="driverClassName" value="org.h2.Driver"/>
  <property name="url" value="jdbc:h2:mem:medical_claims_tracker;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS medical_claims_tracker" />
</bean>

<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate">
    <property name="dataSource" ref="dataSource"/>
    <property name="schemas" value="medical_claims_tracker"/>
    <property name="sqlMigrationPrefix" value="Migration_"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

当我运行单元测试时(通过Eclipse或使用Maven),我得到以下异常:

ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@4cd4544] to prepare test instance [name.hines.steven.medical_claims_tracker.repositories.ExpenseRepositoryTest@1664a9b]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:73)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) …
Run Code Online (Sandbox Code Playgroud)

spring h2 flyway

8
推荐指数
3
解决办法
3万
查看次数

标签 统计

spring ×2

flyway ×1

h2 ×1

java ×1

mocking ×1

mockito ×1

spring-mvc ×1