小编Nic*_*tos的帖子

Java 8 Streams:如何调用Collection.stream()方法并检索具有不同字段的多个聚合值的数组

我将从Java 8中的Stream API开始.

这是我使用的Person对象:

public class Person {

    private String firstName;
    private String lastName;
    private int age;
    private double height;
    private double weight;

    public Person(String firstName, String lastName, int age, double height, double weight) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.age = age;
      this.height = height;
      this.weight = weight;
    }

    public String getFirstName() {
      return firstName;
    }
    public String getLastName() {
      return lastName;
    }
    public int getAge() {
      return age;
    }
    public double getHeight() {
      return height;
    }
    public …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

11
推荐指数
3
解决办法
1373
查看次数

Springboot / Kotlin:测试注入另一个 @ConfigurationProperties 类的类的最佳实践

我有一个 Kotlin 项目。

我创建了一个@ConfigurationProperties类,我想知道单元测试的最佳实践。

以下是我的属性类:

@ConstructorBinding
@ConfigurationProperties(prefix = "myapp")
data class MyAppProperties(
    /**
     * Base path to be used by myapp. Default is '/search'.
     */
    val basePath: String = "/myapp"
)
Run Code Online (Sandbox Code Playgroud)

我在控制器中注入 MyAppProperties:

@RestController
final class MyAppController(
    myAppProperties: MyAppProperties
) {

    ...

}
Run Code Online (Sandbox Code Playgroud)

我想测试我的控制器:

@ExtendWith(MockitoExtension::class)
internal class MyAppControllerTest {

    @Mock
    lateinit var myAppProperties: MyAppProperties

    @InjectMocks
    lateinit var myAppController: MyAppController

    ...

}
Run Code Online (Sandbox Code Playgroud)

但我有以下 Mockito 错误:

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class com.myapp.MyAppProperties
Mockito cannot mock/spy because :
 - final class
Run Code Online (Sandbox Code Playgroud)

解决此问题的最佳解决方案是什么: …

unit-testing mockito kotlin spring-boot configurationproperties

5
推荐指数
0
解决办法
472
查看次数