springboot Junittest 报异常:org.springframework.beans.factory.UnsatisfiedDependencyException:


import com.test.springboot_vue.dao.NodeRepository;
import com.test.springboot_vue.entities.Node;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class) //启动类
@WebAppConfiguration
public class SpringbootVueApplicationTests2 {

    private MockMvc mvc;

    @Autowired
    private WebApplicationContext context;
    @Autowired
    NodeRepository nodeRepository;  //报错是由于这个找不到 

    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.webAppContextSetup(context).build();
        Node node = new Node();
        node.setId(1);
        node.setIpaddr("192.168.1.222");
        node.setLogin("user1");
        node.setPasswd("123456");
        node.setToken("");
        nodeRepository.save(node);
    }

    @Test
    public void testUserController() throws Exception {
        RequestBuilder request = null;
        request = MockMvcRequestBuilders.get("/api/nodes/detail/1");
        mvc.perform(request)
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("{}")));

    }

}


你可能感兴趣的:(springboot)