我是单元测试的新手。我使用TestNG和Mockito进行单元测试。我有 8 个测试用例,当一个接一个地运行测试用例时,它工作正常。之后,我将所有这些方法添加到一个类中。运行测试类后,我得到了断言空异常。我无法找到根本原因。这是我的代码。
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)
所以失败的断言在negTestCheckLoginAlertReportFlagByNo
.null
告诉您的所有内容都是您没有提供自定义消息。
问题是返回来自
loginBean.checkIsReportCreatedByCustomer("1234@1234.com")
false
.
至于为什么会这样,我们不可能帮上忙。 现在由您来确定测试是否有问题或正在测试的代码行为不正确。
如果测试本身确实成功negTestCheckLoginAlertReportFlagByNo
,那么可能是其他一些测试影响了其结果,并且应该使用 TestNG 的优先级或依赖机制来控制测试执行顺序。
作为旁注,我认为以下表达式很难阅读,因为它很容易被误解:
assertNotEquals(false, loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
我建议使用方法assertTrue
,在这种情况下assertFalse
,因为它们更明显一些。
因此,要么:
assertFalse(loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
如果检查应返回false
,或者:
assertTrue(loginBean.checkIsReportCreatedByCustomer("1234@1234.com"));
如果它应该返回true
.