Home  >  Article  >  Java  >  How to solve the java.lang.AssertionError error problem

How to solve the java.lang.AssertionError error problem

WBOY
WBOYforward
2023-04-30 17:37:075505browse

Report java.lang.AssertionError

Report an error when using Assert.assertEquals

How to solve the java.lang.AssertionError error problem

##First of all, we need to understand the usage of Assert

assert

  • If is true, the program continues execution.

  • If it is false, the program throws AssertionError and terminates execution

assert : < ;Error message expression>

  • If is true, the program continues execution.

  • If it is false, the program throws java.lang.AssertionError and outputs .

ctrl click on Assert.assertEquals and find that

How to solve the java.lang.AssertionError error problem

is used to judge the values ​​of two Object types and compare them It is whether the reference addresses are equal, and the contents are not compared:

If the two are consistent, the program continues to run.

If the two are inconsistent, the test method is interrupted and the exception message AssertionFailedError is thrown.

The assertion I wrote is like this:

Assert.assertEquals(7, userList.size());

It is clearly pointed out in the error that Excepted is 7 and actual is 8, so the two values ​​are inconsistent and an error is reported

Therefore Just change 7 to 8

Assert.assertEquals(8, userList.size());

java.lang.AssertionError: Expected:2 Actual:9

I encountered this when unit testing the jdbc code found

The unit test code is as follows

    @Test
    public void testFind() throws Exception {
        //构建测试数据
        //创建目标类的对象
        CategoryDaoImpl dao = new CategoryDaoImpl();
        //调用对象的目标方法
        List<Category> list = dao.find();
        int actual = list.size();
        int expected = 2;
        //使用断言类的方法判断;比较实际和预计的结果
        Assert.assertEquals(actual, expected);
    }

If you also report the same error,

check if the number of data in your database is different from expected 2

Yes It is not the actual value written in the error. For example, my database has 9 entries

The above is the detailed content of How to solve the java.lang.AssertionError error problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete