【发布时间】: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