我将从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) 我有一个 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