Im trying to write a JUnit test for a method in my spring controller, but I cant seem to get the test right. It doesn't seem to autowire the db connection in the controller when i'm testing.
The controller
@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";
}
}
}
and the JUnit test class
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);
}
}
Am I missing something here?