Assertion in Java

   An assertion is a statement in the Java language that enables us to test our assumptions about our program. For example, if we write a method to calculate the money in our pocket, then we might assert that the total value is more than 0.
 

1. Assert forms.

       1) assert Expression1;
       Where the  Expression1is a Boolean expression. When the system run the assert, it evaluate Expression, and if it is false,  it would throw AssertionError with no detail message. Pls noted that AssertionError inherit from Error
 
       2) assert Expression1Expression2;
where: 
  •  Expression1 is a boolean expression.
  • Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.)
 
Pls noted that AssertionError inherit from Error, so we may hv no need to handle, also may catch it as normal Error.
 

2. Putting assert into Code.

1) Internal Invariants
  You should now use an assertion whenever you would have written a comment that asserts an invariant.
if (i % 3 == 0) {
        ...
    } else if (i % 3 == 1) {
        ...
    } else {
        assert i % 3 == 2 : i;
        ...
    }
 
2)  Control-Flow Invariants
  It points out another general area where you should use assertions: place       an assertion at any location you assume will not be reached.
 
void foo() {
        for (...) {
            if (...)
                return;
        }
        assert false; // Execution should never reach this point!
    }
 
3) Preconditions.
 
private  void setDelegationInfoIntoSubmissionDto(…) {
 assert  isf10Snapshot.getIsfDelegation()!=null;     
String delegatorCompanyId = isf10Snapshot.getIsfDelegation().getDelegatorCompanyID();
        submissionDTO.setDelegator(true);
        … 
               }
 

3. Enabling/Disabling Assertions.

1) To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch. You specify the granularity with the arguments that you provide to the switch:
·  no arguments 
   Enables or disables assertions in all classes except system classes.
·  packageName... 
   Enables or disables assertions in the named package and any subpackages.
·  ...
   Enables or disables assertions in the unnamed package in the current working directory.
· className
   Enables or disables assertions in the named class
2) Enable it in oc4j
      Use -esa | -enablesystemassertions to enable system assertions;
      Use -dsa | -disablesystemassertions to disable system assertions
 
3) Enable it in Eclipse
   In run configuration dialog, switch to Arguments tab, input –ea/-da in VM arguments filed.
  

4.Performance

       The Assertion mechanism is as following. When the compiler finds an assertion in a class, it adds a generated static final field named $assertionDiabled to the class. And the assertion itself is compiled into a statement of the form:
      if($assertionDisabled)
           if(!boolean_expression)
                      throw new assertionError(String_Expression);
 
so, if assertion overhead, it would introduce an performance issue into system, no matter the assertion is enabled or disabled.
### 回答1: JSON assertion 是一种测试技术,用于验证 Web 服务返回的 JSON 格式的数据是否符合预期。在 JSON assertion 中,测试人员通过编写断言语句来判断 JSON 数据中的某些属性或字段是否等于预期值,从而确定测试是否通过。 例如,假设我们有一个返回 JSON 格式数据的 API,它返回以下数据: ```json { "name": "John Doe", "age": 30, "email": "johndoe@example.com" } ``` 我们可以通过使用 JSON assertion 来确保返回的数据中包含正确的属性和值。例如,以下是一个使用 JSON assertion 的例子: ```json { "assertions": [ { "path": "$.name", "value": "John Doe" }, { "path": "$.age", "value": 30 }, { "path": "$.email", "value": "johndoe@example.com" } ] } ``` 在这个例子中,我们使用 JSONPath 表达式 `$` 来表示整个 JSON 对象,然后使用 `$.name`、`$.age` 和 `$.email` 来表示 JSON 对象中的属性。每个断言都有一个路径和一个预期值,用于检查 JSON 数据是否符合预期。如果所有断言都通过,则测试被视为通过。 ### 回答2: JSON断言是一种测试技术,用于验证一个Web服务返回的JSON响应是否符合预期的格式和内容。JSON断言通常在API自动化测试中使用,目的是确保API返回的数据与预期结果匹配。 JSON断言的实现通常使用断言库或测试框架提供的函数,比如Java中的JUnit或TestNG,Python中的unittest或pytest等。这些函数通常提供了一些可用于测试JSON响应的方法,如检查JSON对象的键、值是否相等,检查数组的长度,检查嵌套对象或数组的存在等。 使用JSON断言时,首先需要定义预期的JSON响应。可以通过手动编写JSON预期结果,或从一个文件或数据库中读取。然后,通过发送API请求并获得响应的方式获取实际JSON响应。最后,使用断言函数对预期响应和实际响应进行比较。 JSON断言可以用于验证JSON响应的各个方面,例如检查响应状态码、响应头、数据内容等。通过使用JSON断言,可以快速准确地验证API的正确性,并在代码中进行持续集成和自动化测试。 需要注意的是,JSON断言并不是检查API是否正确的唯一方法。还需要考虑其他因素,如请求参数、环境配置等。此外,断言的覆盖范围和准确性也需要根据具体情况进行调整,以确保测试的全面性和准确性。 ### 回答3: JSON断言(JSON assertion)是一种用于验证JSON数据的方法。它可以用于自动化测试中,通过对返回的JSON数据进行断言来判断接口是否按照预期工作。 在进行JSON断言时,我们首先需要明确我们想要验证的内容。这可以包括JSON对象的键值对、数组中的元素、键值对的数量、特定节点的存在与否等等。接下来,我们可以使用断言库、脚本或者测试框架提供的JSON断言函数来进行验证。 常用的JSON断言方法包括: 1. 比较值:使用等于(==)或不等于(!=)运算符来比较JSON字段的值是否符合预期。 2. 包含关系:利用包含运算符(in)来判断数组中是否包含指定元素。 3. 大小比较:使用比较运算符(>、<、>=、<=)来判断数组的长度或对象的键值对数量。 4. 正则表达式:通过正则表达式来验证JSON字段的值是否符合指定的模式。 5. 逻辑判断:使用逻辑运算符(与、或、非)来进行多个JSON字段的联合判断。 举例来说,假设我们的API返回了一个JSON对象,其中包含了用户的姓名和年龄。我们想要验证姓名是否为"John",年龄是否大于等于18。我们可以使用JSON断言来判断这两个条件是否成立。 使用JSON断言可以提高测试的准确性和可靠性,节省了手动验证的时间和精力。同时,它也可以帮助我们快速定位和排查接口返回数据中的问题,并提供更好的错误报告。总之,JSON断言是一种非常有用的工具,可以帮助我们进行自动化测试和保障接口的质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值