小编pgm*_*man的帖子

声纳 - 存储副本 - 不应直接存储或返回可变成员

我有一个列表,它是我班上的私人成员。我使用 getter 和 setter 来获取和设置值。SOnar 引发错误 - 不应直接存储或返回可变成员。

例如:ABC 和 DEF 是两个类。

class ABC{
private List<DEF> defList;
public List<DEF> getDefList() { return defList; }
public void setDefList(List<DEF> defList) { this.defList = defList; }
Run Code Online (Sandbox Code Playgroud)

经过大量的谷歌搜索和搜索,我了解到可以按如下方式更改 getter:

public List<DEF> getDefList() { return new ArrayList<>(defList); }
Run Code Online (Sandbox Code Playgroud)

当我尝试类似地使用 setter 时,

public void setDefList(List<DEF> defList) { this.defList.addAll(defList); }
Run Code Online (Sandbox Code Playgroud)

然后变量开始显示

'private field 'defList' is never assigned.
Run Code Online (Sandbox Code Playgroud)

我可以知道当它是一个列表时的正确方法吗(另一个类的列表)

注意:Prasad Karunagoda 和 Leo Aso 的答案都有效。我不能将两者都标记为已接受的答案。所以在这里做个笔记

java arraylist mutable sonarqube

9
推荐指数
1
解决办法
1万
查看次数

在Spring Boot执行器信息端点中显示构建时间戳

我正在使用Spring Boot执行器来获取我的应用程序的信息。

我在pom.xml中添加了Spring Boot执行器依赖项,并在属性文件中添加了以下几行:

info:
   app:
     name: @project.name@
     description: @project.description@
     version: @project.version@
Run Code Online (Sandbox Code Playgroud)

我从pom.xml获取值:项目的名称,描述和版本。我还想获得构建时间并将其显示在/info端点上。

有什么建议么?

我是否还应该为此更改pom.xml文件?我尝试使用:

info.app.build-time=@build-time@
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

谢谢

java spring-mvc maven spring-boot-actuator

5
推荐指数
1
解决办法
2919
查看次数

SonarQube错误:重构此方法最多抛出一个已检查的异常

我正在使用SonarQube,它显示以下错误:

Public methods should throw at most one checked exception.

// Noncompliant
public void delete() throws IOException, SQLException { /* ... */ }

// Compliant
public void delete() throws SomeApplicationLevelException { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

这是否意味着,SomeApplicationLevelException是一个父类,IOException并且SQALException是从它派生出来的?我们应该抛出父类异常?从而坚持只抛出1个检查异常的方法?

因为我有2个例外,我已经定义了例如说Exception1Exception2扩展Exception.我的方法说,sampleMethod()抛出它们,即

public void sampleMethod() throws Exception1, Exception2 {
}
Run Code Online (Sandbox Code Playgroud)

这里显示错误.所以我应该有一个类作为父(说MainException),并获得Exception1Exception2从它扔父异常类?如下所示:

public void sampleMethod() throws MainException {
}
Run Code Online (Sandbox Code Playgroud)

以上解决方案是否合适?

java exception-handling sonarqube

4
推荐指数
2
解决办法
4558
查看次数

创建 http 响应作为 mockito 的返回值

我是 Mockito 测试的新手,正在测试 GET 请求。

我已经模拟了这个调用,但现在我需要返回,HttpResponse<Cars[]>因为这些值稍后会在 for 循环中使用。

如何创建一个HttpResponse包含Car对象数组的虚拟对象。

类中使用Get请求的方法CarProvider是:

public HttpResponse<Cars[]> getCars(String carId, String carname) {
    return Unirest.get(endpointUrl)
          .header("Accept", MediaType.APPLICATION_JSON)
          .queryString("carId",cardId)
          .queryString("carname",carname)
          .asObject(Cars[].class);
}
Run Code Online (Sandbox Code Playgroud)

CarsUsers在以下方法的另一个类中使用:

public List<Cars> getCars(String carId, String carname) {
  HttpResponse<Cars[]> response = carProvider.getCars(carId, carname);
  for(Cars car: response.getBody){
     //use car and do something
}
Run Code Online (Sandbox Code Playgroud)

我的测试方法如下:

public class TestClass {
    @Mock
    HttpResponse<Cars[]> httpresponse;
    @Mock
    CarsProvider carsProvider;

    @Mock
    CarsUser carsUser;

    @Test
    public void getCarsTest(){
     Mockito.when(carsUser.getCars(Matchers.anyString(), 
     Matchers.anyString())).thenReturn(getDummyCarsList());

     Mockito.when(carsProvider.getCars(Matchers.anyString(), 
     Matchers.anyString())).thenReturn(httpResponse);

     } …
Run Code Online (Sandbox Code Playgroud)

java rest junit httpresponse mockito

3
推荐指数
1
解决办法
2万
查看次数