Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用DAO mock编写集成测试控制器?_Java_Spring Boot_Spring Test_Spring Restcontroller_Springmockito - Fatal编程技术网

Java 无法使用DAO mock编写集成测试控制器?

Java 无法使用DAO mock编写集成测试控制器?,java,spring-boot,spring-test,spring-restcontroller,springmockito,Java,Spring Boot,Spring Test,Spring Restcontroller,Springmockito,我发疯了,我尝试了各种测试运行者和可能的测试注释的所有可能组合,我需要的最接近的解决方案如下: @RunWith(SpringRunner.class) @SpringBootTest(classes = {MyApplication.class}) @WebAppConfiguration public class MyControllerTest { MockMvc mockMvc; // My DAO is an interface extending JpaRepos

我发疯了,我尝试了各种测试运行者和可能的测试注释的所有可能组合,我需要的最接近的解决方案如下:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class})
@WebAppConfiguration
public class MyControllerTest {

    MockMvc mockMvc;

    // My DAO is an interface extending JpaRepository
    @Mock
    MyDAO myDAO;

    @Autowired
    WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        List<MyItem> myItems = new ArrayList(){{
            // Items init ...
        }}
        Mockito.when(myDAO.findAll()).thenReturn(myItems);
        /* Other solution I tried with different annotations: 
        * given(myDAO.findAll()).willReturn(myItems);
        * this.mockMvc = MockMvcBuilders.standaloneSetup(myController).build();
        */
        this.mockMvc = webAppContextSetup(webApplicationContext).build();

    }

    @After
    public void tearDown() throws Exception {
//        Mockito.reset(myDAO);
    }

    @Test
    public void getItems() {
        String res = mockMvc.perform(get("/items"))/*.andExpect(status().isOk())*/.andReturn().getResponse().getContentAsString();
        assertThat(res, is("TODO : string representation of myItems ..."));
        assertNull(res); // For checking change in test functionning
    }
}
我并没有提出例外,我的刀并没有被嘲笑,我不知道怎么做,尽管我发现了所有的图图。 我找到的最接近我需要的图托是这个,但还不够,因为他想把模拟服务注入控制器中,为了测试控制器,我想把模拟DAO注入注入控制器中的实际服务中(我想测试控制器+服务)

在我看来,我已经通过在测试类上使用注释做到了这一点,该注释指定了什么类必须在测试模式下由spring应用程序实例化,什么类必须被模拟,但我不记得'-.-'

需要你的帮助,这让我很粗鲁


非常感谢

对于@Mock注释,您需要额外的初始化:

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }
或者将runner更改为
@RunWith(MockitoJUnitRunner…
,但不确定本例中的spring上下文初始化。

我最终喜欢这样做(但不满意,因为它不模拟HSQLDB数据库,而是创建一个测试数据库),而我想模拟DAO:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MySpringApplication.class})
@WebAppConfiguration
public myTestClass {
    MockMvc mockMvc;

    @Autowired
    ItemDAO itemDAO;

    @Autowired
    WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        this.mockMvc = webAppContextSetup(webApplicationContext).build();
    }

    @After
    public void tearDown() throws Exception {
        itemDAO.deleteAll();
    }

    @Test
    public void testMethod() {
        // DB init by saving objects: create a item and save it via DAO, use real test DB

        // Only mocking about rest call:
        String res = mockMvc.perform(get("/items")).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
    }

}

尝试编写
Mockito.when(myDAO.findAll()).thenReturn(myItems);
internal@Test不在安装程序中。还要确保@Test中可以访问
myItems
。是的,如果使用另一个运行程序,将会出现问题(我听说@Delegate注释使用2个运行程序,但很复杂)。不幸的是,我已经尝试过了,但没有改变,我的DAO没有返回/抛出我告诉它的内容。您需要排除扫描DAO包,并使用“Mockito.mock(ItemDAO.class)”手动创建DAO bean。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MySpringApplication.class})
@WebAppConfiguration
public myTestClass {
    MockMvc mockMvc;

    @Autowired
    ItemDAO itemDAO;

    @Autowired
    WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        this.mockMvc = webAppContextSetup(webApplicationContext).build();
    }

    @After
    public void tearDown() throws Exception {
        itemDAO.deleteAll();
    }

    @Test
    public void testMethod() {
        // DB init by saving objects: create a item and save it via DAO, use real test DB

        // Only mocking about rest call:
        String res = mockMvc.perform(get("/items")).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
    }