Home > Java > javaTutorial > body text

How to solve the java.lang.AssertionError error problem

WBOY
Release: 2023-04-30 17:37:07
forward
5509 people have browsed it

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());
Copy after login

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());
Copy after login

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);
    }
Copy after login
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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template