小编Akk*_*rek的帖子

如何模拟REST模板交换?

我有一项服务,我需要通过休息询问外部服务器以获取一些信息:

public class SomeService {

    public List<ObjectA> getListofObjectsA() {
        List<ObjectA> objectAList = new ArrayList<ObjectA>();
        ParameterizedTypeReference<List<ObjectA>> typeRef = new ParameterizedTypeReference<List<ObjectA>>() {};
        ResponseEntity<List<ObjectA>> responseEntity = restTemplate.exchange("/objects/get-objectA", HttpMethod.POST, new HttpEntity<>(ObjectAList), typeRef);
        return responseEntity.getBody();
    }
}
Run Code Online (Sandbox Code Playgroud)

我该如何编写JUnit测试getListofObjectsA()

我试过以下:

@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
    private MockRestServiceServer mockServer;

    @Mock
    private RestTemplate restTemplate;

    @Inject
   private SomeService underTest;

@Before
public void setup() {
    mockServer = MockRestServiceServer.createServer(restTemplate);
    underTest = new SomeService(restTemplate);
    mockServer.expect(requestTo("/objects/get-objectA")).andExpect(method(HttpMethod.POST))
            .andRespond(withSuccess("{json list response}", MediaType.APPLICATION_JSON));
}

    @Test
    public void testGetObjectAList() {
    List<ObjectA> res = underTest.getListofObjectsA();
    Assert.assertEquals(myobjectA, …
Run Code Online (Sandbox Code Playgroud)

java junit mockito spring-boot

23
推荐指数
5
解决办法
6万
查看次数

如何在foreach方法中从流中删除对象?

我必须数组:arrAarrB.arrA并且arrB是不同类型和add函数的对象列表将对象转换A为对象B.我想将arrA中的每个对象添加到arrB并从arrA中删除该对象.我试图通过流来做到这一点:

arrA.stream().foreach(c -> {arrB.add(c); arrA.remove(c);});
Run Code Online (Sandbox Code Playgroud)

当我执行此操作时,会发生两件事:

  1. 并非所有对象都从arrA传递到arrB.
  2. 在几次迭代之后抛出空指针异常.

我猜这是因为每次remove()调用后数组的长度都会减少,并且迭代计数器会增加(只有奇数索引下的对象才能传递给arrB)

现在我可以通过在一个流调用中复制数组然后在第二个流调用中删除对象来解决这个问题,但这对我来说似乎不正确.

什么是这个问题的正确解决方案?

编辑.附加信息:如果先前已过滤,则在实际实施中显示此列

arrA.stream().filter(some condition).foreach(c -> {arrB.add(c); arrA.remove(c);});
Run Code Online (Sandbox Code Playgroud)

并且它被调用几次以将不同条件的元素添加到不同的列表(arrC, arrD等),但每个对象只能在一个列表上

java arrays lambda java-8

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

如何为多模块项目配置 Flyway?

我有一个多模块项目,其中一个模块负责处理我的迁移,所以我的项目结构如下所示:

project
|
|-> application
|-> approval-management
|-> database
|-> security
...
Run Code Online (Sandbox Code Playgroud)

pom.xml的数据库模块如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <artifactId>application</artifactId>
    <groupId>com.test</groupId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>database</artifactId>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>


<profiles>
    <profile>
        <id>local</id>
        <properties>
            <activatedProperties>local</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <activatedProperties>test</activatedProperties>
        </properties>
    </profile>
</profiles>


<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.1.2.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId> …
Run Code Online (Sandbox Code Playgroud)

spring multi-module flyway spring-boot

7
推荐指数
0
解决办法
1283
查看次数

“ HttpServletResponse已提交”是什么意思?

如标题中所述,提交HttpServletResponse是什么意思?

我有一些请求拦截器,扩展HandlerInterceptorAdapter,覆盖了postHandle方法。后处理方法采用parameter final HttpServletResponse response。在方法主体中有一条if语句检查if response.isCommitted(),该语句究竟执行什么操作?

private static final String voidResponse = "null";

@Override
    public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView) throws IOException {
        if (!response.isCommitted()) {
            if (DefaultServletHttpRequestHandler.class == handler.getClass()) {
                return;
            }
            response.setStatus(200);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            try (final Writer writer = response.getWriter()) {
                writer.write(voidResponse);
            }
            log.info("void method called, respond with 200 and null");

            response.flushBuffer();
        }
    }
Run Code Online (Sandbox Code Playgroud)

java spring servlets httprequest

6
推荐指数
1
解决办法
4714
查看次数

Maven 无法识别 Swagger 依赖项

我正在尝试将 springfox/swagger 添加到我的 Spring Boot 应用程序中,但是当添加到 pom 文件中时,maven 无法识别依赖项的版本。不幸的是我找不到其背后的原因。这是我的 pom 文件的片段:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.0.2</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

版本标记为红色,表示 Maven 无法识别。这种行为背后的原因可能是什么?我正在使用 Maven 3.3.9。

java maven swagger swagger-ui springfox

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

连接到 Revolut 沙盒

我正在尝试创建与 Revolut 的客户端连接。我正在遵循他们的教程,但是我被困在Exchange Authorization Code上。

到目前为止我所做的:

  1. 在他们的sanbox上创建了企业帐户
  2. 在这里添加了我的公钥https://sandbox-business.revolut.com/settings/api
  3. 通过启用 API 访问您的帐户激活了我的 API 证书(我完成了所有步骤并获得了访问现已启用的信息),但是我仍然可以Access is not enabled在我的 API 证书上看到
  4. 创建 JWT 令牌并使用我的私钥对其进行签名(使用https://jwt.io/进行验证)
  5. 像这样发送访问令牌请求(通过邮递员完成):

    curl --request POST \
      --url https://sandbox-b2b.revolut.com/api/1.0/auth/token \
      --header 'Accept: */*' \
      --header 'Accept-Encoding: gzip, deflate' \
      --header 'Cache-Control: no-cache' \
      --header 'Connection: keep-alive' \
      --header 'Content-Length: 596' \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --header 'Host: sandbox-b2b.revolut.com' \
      --header 'User-Agent: PostmanRuntime/7.20.1' \
      --header 'cache-control: no-cache' \
      --data 'grant_type=authorization_code&code=oa_sand_xxx&client_id=xxx&client_assertion_type=xxx&client_assertion=xxx …
    Run Code Online (Sandbox Code Playgroud)

authorization jwt auth0

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

从堆栈和堆中释放内存

如何释放为已知大小的数组存储的内存?

下面的代码打印4次"构造函数调用!" 但只有一次"Destructor叫!" 这是否意味着整个阵列的内存已被解除分配?

class Box
{
   public:
      Box() { 
         cout << "Constructor called!" <<endl; 
      }
      ~Box() { 
         cout << "Destructor called!" <<endl; 
      }
};

int main( )
{
   Box myBoxArray[4];

   delete myBoxArray; // Delete array

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我myBoxArray动态声明,如下面的代码所示,析构函数被调用4次.

class Box
{
    public:
      Box() { 
         cout << "Constructor called!" <<endl; 
      }
      ~Box() { 
         cout << "Destructor called!" <<endl; 
      }
}; 

int main( )
{
   Box* myBoxArray = new Box[4];

   delete [] myBoxArray; // Delete array

   return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor destructor allocation

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

Junit测试中的Java ArrayList填充了单值

我有以下Junit类进行测试(测试不包括在这种情况下它们并不重要):

public class BoardImplTest {

List<Coords> startingCells = new ArrayList<>();
Coords cellCoords = new Coords();


private void addCell(int col, int row){
    cellCoords.setCols(col);
    cellCoords.setRows(row);

    startingCells.add(cellCoords);
}

private void testSetup() {
    addCell(3,2);       

    addCell(4,2);

    addCell(1,3);

    addCell(2,3);

    addCell(3,3);

    addCell(4,4);   

}
Run Code Online (Sandbox Code Playgroud)

运行之后,数组startingCells只填充一个值:

  1. 在第一个addCell(3,2);数组的大小= 1后,其中一个元素的坐标为(3,2)
  2. addCell(4,2);数组后大小= 2,两个元素的坐标为(4,2)
  3. addCell(1,3);阵列具有尺寸= 3的COORDS的三个元件(1,3)

等等.

在每次调用addCell之后,数组会增加其大小,并且所有字段都填充了作为参数传递的值,并且我希望这些值仅作为Array的最后一个元素添加.问题出在哪儿?

java arrays junit

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

在运算符重载中使用"const"作为参数

我试图理解运算符重载,在我使用的教程中有一个重载"+"运算符的例子,用于添加两个对象.

  Box operator+(const Box& b)
  {
     Box box;
     box.length = this->length + b.length;
     box.breadth = this->breadth + b.breadth;
     box.height = this->height + b.height;
     return box;
  }
Run Code Online (Sandbox Code Playgroud)

为什么参数需要是对象的const引用?

c++ const operator-overloading

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

为唯一的字符串种子生成唯一的 Long

我需要Long从可以是任何长度的字符串种子中生成一个长度为 10 位或更多位的唯一值。在正常情况下这是不可能的,因为有StringLong可以存储的更多的排列,但是我知道系统不会生成Strings超过我可以存储的更多Long,在这种情况下我如何Long为每个生成唯一的String

我不能使用动态完美散列,因为太耗时,我不能使用最小完美散列函数,因为我不希望生成的数字是顺序的。

编辑:我无法存储有关已处理字符串的任何信息,包括它们的数量

java string algorithm long-integer

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