there is a project a simple rest service: bitbakt
All code should be covered with Junit tests. I have never done this before, but I wrote a small test for one of the controller's methods (in the code, the class of the test is not named correctly). Tell me what's wrong with it, how can it be improved, and why does nullPointerExeption come out when it starts?
Here is the test code, sobsna. (What he is testing, I think it will be more convenient to look in the repository):
public class DepartmentControllerTest { private final List<Department> list = new ArrayList<>(); @Before public void initDepartment(){ Department department = new Department(); department.setId(2L); department.setDepartmentName("Java"); department.setEmployeesInThisDepartment(null); list.add(department); } @Test public void testList() throws Exception { DepartmentService departmentService = new DepartmentService(); when(departmentService.getAll()).thenReturn(list); DepartmentController departmentController = new DepartmentController(); ReflectionTestUtils.setField(departmentController, "departmentService", departmentService); ExtendedModelMap uiModel = new ExtendedModelMap(); uiModel.addAttribute("departments", departmentController.getAllDepartments()); Department department = (Department) uiModel.get("departments"); assertEquals(1, department.getEmployeesInThisDepartment().size()); }
java.lang.NullPointerException at com.service.DepartmentService.getAll (DepartmentService.java:24) at dao.DepartmentDaoTest.testList (DepartmentDaoTest.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native.) at sun.re NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.'s ours ours ours is ours ours ours ours our of ours ours. .FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java:50) at org.junit.internal. 47) at org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:26) at org.junit.runners .ParentRunner.runLeaf (ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4 ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner. .schedule (ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren (ParentRunner.java:288) at org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:58) at org.junit.run .ParentRunner $ 2.evaluate (ParentRunner.java:268) at org.junit.runners.ParentRunner.run (ParentRunner.java uniform63) at org.junit.runner.JUnitCore.run (JUnitCore.java:137) at com.intellij .junit4.JUnit4IdeaTestRunner.startRunnerWithArgs (JUnit4IdeaTestRunner.java:119) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs (JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart (JUnitStarter.java:234 ) at com.intellij.rt.execution.junit.JUnitStarter.main (JUnitStarter.java:74) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMet hodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (.. .application.AppMain.main (AppMain.java:144)
Process finished with exit code -1