断言在运行的测试组中为null

时间:2018-05-22 04:28:14

标签: java exception mockito testng

是单位测试的新手。我使用TestNG和Mockito进行单元测试。我一个接一个地运行测试用例时有8个测试用例,它工作正常。之后我在一个类中添加了所有这些方法。运行测试类后,我得到了Assertion null异常。我找不到根本原因。这是我的代码。

public class TestLoginController {

@Mock
DBResourseLoader dbResourceLoader;

@Mock
MessageLoader messageLoader;

@Mock
Logger logger;

@Mock
Properties properties;

@Mock
Properties databaseProperties;

@Mock
DBConnector connector;

@Mock
Connection connection;

@Mock
PropertyConfigurator propertyConfigurator;

@InjectMocks
LoginBean loginBean;

@Mock
CommonDAO commonDAO;

@Mock 
PreparedStatement preparedStatement;

@Mock
ResultSet resultSet;




@BeforeClass
public void setup() {
    try {

        MockitoAnnotations.initMocks(this);
        when(messageLoader.getMessageStatement("")).thenReturn("");
        when(dbResourceLoader.getJdbcDatasource()).thenReturn(properties);
        when(dbResourceLoader.getLogger()).thenReturn(databaseProperties);
        when(dbResourceLoader.getLogger(Logger.class)).thenReturn(logger);

        when(connector.getConnection(true)).thenReturn(connection);
    }catch(Exception e) {
        e.printStackTrace();
    }
}


@BeforeMethod
public void setBeforeValues() {


}

@Test
public void negTestCheckIsReportCreatedByCustomerReportFlagByNo() {
    try {
        when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("NO");
        reset(commonDAO);
        assertNotEquals(false, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
    }catch(Exception e) {
        e.printStackTrace();
    }
}

@Test
public void posTestCheckIsReportCreatedByCustomerReportFlagByNo() {
    try {

        when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("NO");
        assertEquals(true, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));

    }catch(Exception e) {
        e.printStackTrace();
    }
}

@Test
public void negTestCheckIsReportCreatedByCustomerByReportFlagYes() {
    try {
        when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("YES");
        when(commonDAO.getCustomerReportCreationValue("1234@gmail.com")).thenReturn(false);
        assertNotEquals(true, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
    }catch(Exception e) {
        e.printStackTrace();
    }
}

@Test
public void posTestCheckIsReportCreatedByCustomerByReportFlagYes() {
    try {
        when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("YES");
        when(commonDAO.getCustomerReportCreationValue("1234@1234.com")).thenReturn(true);
        assertEquals(true, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
    }catch(Exception e) {
        e.printStackTrace();
    }
}

@Test
public void negTestCheckLoginAlertReportFlagByNo() {
    try {
        when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("NO");
        assertNotEquals(false, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
    }catch(Exception e) {
        e.printStackTrace();
    }
}

@Test
public void posTestLoginAlertCustomerReportFlagByNo() {
    try {
                    when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("NO");
        assertEquals(true, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));

    }catch(Exception e) {
        e.printStackTrace();
    }
}

@Test
public void negTestLoginAlertByReportFlagYes() {
    try {
        when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("YES");
        when(commonDAO.getCustomerReportCreationValue("1234@gmail.com")).thenReturn(false);
        assertNotEquals(true, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
    }catch(Exception e) {
        e.printStackTrace();
    }
}

@Test
public void posTestCheckLoginAlertByReportFlagYes() {
    try {
        when(commonDAO.getLoginControllerValue("LOGIN_CONTROLLER")).thenReturn("YES");
        when(commonDAO.getCustomerReportCreationValue("1234@1234.com")).thenReturn(true);
        assertEquals(true, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
    }catch(Exception e) {
        e.printStackTrace();
    }
}

}

例外是

FAILED: negTestCheckLoginAlertReportFlagByNo
java.lang.AssertionError: null
at org.testng.Assert.fail(Assert.java:93)
at org.testng.Assert.assertNotEquals(Assert.java:897)
at org.testng.Assert.assertNotEquals(Assert.java:902)
at 
com.test.login.controller.TestLoginController.
negTestCheckLoginAlertReportFlagByNo(TestLoginController.java:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.
invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.
invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

2 个答案:

答案 0 :(得分:0)

因此失败的断言在negTestCheckLoginAlertReportFlagByNo。所有null都告诉您,您没有提供自定义消息。

问题是从

返回
loginBean.checkIsReportCreatedByCustomer("1234@1234.com")

false

至于为什么我们不可能提供帮助。现在由您决定测试是否有问题或者测试的代码是否表现不正确。

答案 1 :(得分:0)

如果测试negTestCheckLoginAlertReportFlagByNo本身确实成功,则可能是其他一些测试影响其结果,并且应使用TestNG的优先级或dependency机制来控制测试执行顺序。

作为旁注,我认为以下表达难以阅读,因为它很容易被误解:

assertNotEquals(false, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));

我建议在这种情况下使用方法assertTrueassertFalse,因为它们更明显一些。

所以,要么:

assertFalse(loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));

如果支票应该返回false,或者:

assertTrue(loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));

如果它应该返回true