无法通过将日期硬编码到构造函数来执行测试用例

时间:2015-09-01 07:12:56

标签: java junit junit4

我通过模拟连接对象为我的java daoImpl类编写了一个测试用例,但是,这是扭曲。我一直在硬编码值,以便我可以将这些记录与我从数据库中获取的记录进行比较并最终断言它们。

Error: java.lang.Error: Unresolved compilation problems: 
    The constructor Employee(String, String, String, String, String, String, int, int, String, int, boolean, int) is undefined
    The constructor Employee(String, String, String, String, String, String, int, int, String, int, boolean, int) is undefined

at com.cerner.devcenter.dao.junit.OrganizationDaoImplTest.testGetAllEmployeeDetails(OrganizationDaoImplTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

识别TestClass:

public class OrganizationDaoImplTest {

    private SessionFactory sessionFactory;
    private Session session;
    private Query query;
    private OrganizationDaoImpl organizationDaoObj;

    String organizationId = "DevCenter01";
    boolean isActive = true;

    @Before
    public void setUp() {
        sessionFactory = mock(SessionFactory.class);
        session = mock(Session.class);
        query = mock(Query.class);
    }

    @Test
    public void testGetAllEmployeeDetails() throws ParseException {
        String hql = "from Employee where Organization_Id = organizationId and IsActive = isActive";
        List<Employee> expectedEmployeeList = new ArrayList<Employee>();
        expectedEmployeeList
                .add(new Employee("AD042997", "Anitha", "D", "AD042999", new SimpleDateFormat("2015-02-12"),
                        "DevCenter01", "anitha@cerner.com", 805678993, 987652134, "C-1234", 7, true, 1));
        expectedEmployeeList.add(new Employee("AS042987", "Asha", "S", "AD042999",
                "DevCenter01", "asha@cerner.com", 434343434, 676990909, "C-4567", 7, true, 3));
        expectedEmployeeList
                .add(new Employee("HN099239", "Heena", "N", "AD042999",
                        "DevCenter01", "heena@cerner.com", 873847399, 486584658, "C-7432", 7, true, 7));

        organizationDaoObj = new OrganizationDaoImpl();
        Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
        Mockito.when(session.createQuery(hql)).thenReturn(query);
        Mockito.when(query.list()).thenReturn(expectedEmployeeList);
        List<Employee> actualEmployeeList = organizationDaoObj.getAllEmployeeDetails();
        assertNotNull(actualEmployeeList);
    }
}

这里的日期被视为String而不是Date,我尝试使用Date,SimpleDateFormat,两者似乎都不起作用。

我的构造函数:

public Employee(String employeeId, String firstName, String lastName, String reportingManagerId, SimpleDateFormat dateOfJoining,
            String organizationId, String emailId, int mobileNumber, int officePhoneNo, String seatLocation,
            int jobLevel, boolean isActive, int id) {
        this.id = id;
        this.employeeId = employeeId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.reportingManagerId = reportingManagerId;
        this.dateOfJoining = dateOfJoining;
        this.organizationId = organizationId;
        this.emailId = emailId;
        this.mobileNumber = mobileNumber;
        this.officePhoneNo = officePhoneNo;
        this.seatLocation = seatLocation;
        this.jobLevel = jobLevel;
        this.isActive = isActive;
    }

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

在这两行中,

    expectedEmployeeList.add(new Employee("AS042987", "Asha", "S", "AD042999",
            "DevCenter01", "asha@cerner.com", 434343434, 676990909, "C-4567", 7, true, 3));
    expectedEmployeeList
            .add(new Employee("HN099239", "Heena", "N", "AD042999",
                    "DevCenter01", "heena@cerner.com", 873847399, 486584658, "C-7432", 7, true, 7));

您错过了构造函数的SimpleDateFormat参数。