2

1. The run in the background unit test is different from the debug mode test result

image.png

Currently testing the flow chart of the update method under the controller controller
image.png

Currently the problem is with parameter capture. In debug mode, when I execute line by line, when I execute the second line of entityArgumentCaptor.getValue() below, the test reports that does not capture the data .

1 Mockito.verify(this.Service).update(longArgumentCaptor.capture(),
        entityArgumentCaptor.capture());
2 Assertions.assertEquals(oldEntity.getName(), entityArgumentCaptor.getValue().getName());

Let's see the error
image.png

It says the parameter is not captured, probably
1. Maybe does not use argument.captrue() in verify() .
2. It is possible that capture() used by in a stub, but is not called. And it is recommended to use capture() only in verify().

Finally, a nice example is given:
image.png

But compared with the above code, it is found that the usage of is completely consistent with the recommended .

Then I tried to test directly with run mode, and it passed.
image.png

Finally, after testing for a long time, the reason for the error is that I did not write the two parameters in verify() in the same line , as shown in lines 204 and 205
image.png

When I did not write the parameters on the same line, mode, click next, jump to line 204, then jump to line 205, and then jump back to line 204 for some reason, . And line 204 only captures the id parameter.

After testing, did not capture the parameters.

The guess may be because debug executes the statement line by line. When the parameters are not written on the same line, the execution may go wrong and the statement fails directly.

After , put the parameters after the same line, and the execution result in debug mode is correct.

2. The jpa parameter is not allowed to be null

Scenario: All data is displayed when the query parameter name is empty, and the query condition name is null when the front-end is passed to the back-end
image.png
Found an error while testing
image.png

Query defined by jpa

Page<PropertyCompany> findAllByNameContainingAndDeletedIsFalse(String name, Pageable pageable);

Reason: The findByXXX parameter of jpa is not allowed to be null.

Solution:
1: The service layer adds judgment, and when it is null, it is set to an empty string.

public Page<PropertyCompany> page(String name, @NotNull Pageable pageable) {
    if (name == null) {
      name = "";
    }

2. Custom query:

default Page<PropertyCompany> findAll(String name, @NotNull Pageable pageable) {
        Specification<PropertyCompany> specification = PropertyCompanySpecs.containingName(name);
        return this.findAll(specification, pageable);
    }

public class PropertyCompanySpecs {
    public static Specification<PropertyCompany> containingName(String name) {
        if (name != null && !name.trim().isEmpty()) {
            return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.like(root.get("name").as(String.class), String.format("%%%s%%", name.trim()));
        } else {
            return Specification.where(null);
        }
    }
}

What if you do want to query fields whose name is null?

Can use: findByNameIsNull is equivalent to sql statement where name is null

Three pass-by-value problem

The background is that the parent component in angular calls the child component, and lets the child component manipulate the incoming array.

parent component : defines villages: Village[];
and passed it to the child component in the v layer

<component [setVillages]="villages"></component>`

Subcomponent: In order to avoid errors due to the incoming villages being undefined when using villages.push and other operations, do the following:

if (village === undefined) {
  villages = new Array<Village>();
}

Result: The last operation of the child component on the villages does not affect the villages of the parent component, and the villages in the parent component are still undefined.

Solution: The initial variable that defines villages in the parent component should be defined like this:
villages = new Array<Village>();

It is currently believed that: similar to the C language, if the parameters of the function do not pass in the parameter address, it means that the operation is only a copy of the data. If the address of the parameter is passed in, it means that the operation is the real incoming data, and the variable outside the function will also change accordingly.
Here, if the array is opened in the child component new Array(), it means that the memory exists in the child component, and the operation of the data does not affect the data of the parent component. If the array is opened up in the parent component, it means that the child component operates The data of the parent component.
image.png


weiweiyi
1k 声望123 粉丝