【问题标题】:Spring data autowired db connection not working through JUnit testSpring数据自动连接的数据库连接无法通过JUnit测试工作
【发布时间】:2016-12-19 01:02:45
【问题描述】:

我正在尝试为我的 spring 控制器中的方法编写 JUnit 测试,但我似乎无法正确测试。在我测试时,它似乎没有自动连接控制器中的数据库连接。

控制器

@Controller
public class PollController {

    @Autowired
    private PollRepository pollrepo;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String getPoll(@PathVariable String id, Model model) {
        try {
            Poll poll = pollrepo.findById(id);
            if(poll == null) throw new Exception("Poll not found");
            model.addAttribute("poll", poll);
            return "vote";
        } catch (Exception e) {
            return "redirect:/errorpage";
        }
    }
}

和 JUnit 测试类

public class PollControllerTest {

    public PollControllerTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }


    @Test
    public void testGetPoll() {
        System.out.println("getPoll");
        String id = "5856ca5f4d0e2e1d10ba52c6";
        Model model = new BindingAwareModelMap();
        PollController instance = new PollController();
        String expResult = "vote";
        String result = instance.getPoll(id, model);
        assertEquals(expResult, result);
    }
}

我错过了什么吗?

【问题讨论】:

    标签: java spring spring-mvc junit spring-data


    【解决方案1】:

    它不会autowire 数据库连接,因为您的junit 中的controller 实例不是由spring 容器管理的。

    您已经使用关键字new 创建了PollController 的实例,因此它不是spring 托管的bean。

    PollController instance = new PollController();
    

    我建议使用 @RunWith(SpringJUnit4ClassRunner.class) 注释您的测试类并注入控制器进行测试,

    @RunWith(SpringJUnit4ClassRunner.class)
    class PollControllerTest {
    
        //Object under test
        @Autowired
        PollController instance;
    

    【讨论】:

    • 当我尝试这样做时,它给了我以下错误:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'poll.PollControllerTest'的bean时出错:通过字段'instance'表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“poll.PollController”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
    • 添加您的配置 xml 文件。我怀疑你没有使用注解配置和组件扫描配置。
    • 我好像没有,所以这可能是问题所在?
    • @Pegaseus 您需要使用配置来配置您的控制器和其他组件以在 Spring 应用程序上下文中可用。你需要处理这些事情。但是,如果您发现与 junit 相关的答案有帮助,请接受,以便对其他人也有用。
    • 我该怎么做,因为我有点迷失在 Spring 中,刚开始使用它,它让我感到困惑。
    【解决方案2】:

    是的。 pollrepo 是一个外部依赖项,因此您需要在测试类中模拟它。参考下面的代码。

    class PollControllerTest {
    
        //Object under test
        PollController instance;
    
        @Mock
        private PollRepository pollrepo;
    
        @Before
        public void setUp() {
    
            instance = new PollController();
    
            ReflectionTestUtils.setField(instance, "pollrepo", pollrepo);
        }
    
        //Tests goes here
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2021-03-15
      • 2015-12-24
      • 2020-07-23
      • 1970-01-01
      相关资源
      最近更新 更多