我在尝试 Spock 并在编写控制器测试时遇到了一个有趣的问题。
WebMvcTest(value = SomeController.class)
@AutoConfigureMockMvc
@ActiveProfiles(value = "restapi")
@Import(value = SecurityConfiguration)
class AccountBalanceControllerTest extends Specification {
@Autowired
SomeController someController
@MockBean
SomeService someService
def "lets test it" {
given:
someService.findAllByName(_) >> ["Some", "Work"]
when:
def response = mockMvc.perform(get("/v1/someName/545465?fast=false").with(user("mvc-test").roles("SOME_ACCOUNTS")))
then:
response.andExpect(status().isOk())
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是SomeService实例上的模拟方法不起作用,因为它使用不同的 Mock 类来模拟类的实例SomeService 。我在设置中使用来自 Spock 的静态 Mock 方法,然后使用 setterSomeService在控制器中设置。我的问题是有什么优雅的方法可以用于@MockBeanSpockSpecification测试。
interface A{
void some();
}
@Component
class B implements A{
@override
some(){
}
}
@Component
class C implements A{
@override
some(){
}
}
Class D {
@Autowired
List<A> somes;//will it have the instances of both
}
Run Code Online (Sandbox Code Playgroud)
我正在开发一个项目,我们有多个类实现相同的接口.如何让D类中的列表包含B类和C类的bean?
我必须存储在更新的时间dynamodb,经历的文档时dynamodb发现,如果我们将其存储在ISO 8601的格式,我们可以建立一个索引,但在应用程序我有java.sql.timestamp。我怎样才能转换它?
我有以下对象的列表.使用StreamAPI,我可以得到User它出现的频率数List吗?
public class User {
string name;
int age;
}
Run Code Online (Sandbox Code Playgroud) 我对 python 很陌生,下面是我为将日期 obj 转换为 ISO 格式而编写的用于转换的 java 代码。我想要类似的 python 代码我已经搜索了很长一段时间并尝试了一些解决方案,但到目前为止没有一个有效。
private String getISOTimestamp(Long timeStamp) {
Date date = new Date(timeStamp);
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String isoTS = sdf.format(date);
return isoTS;
}
Run Code Online (Sandbox Code Playgroud) java ×3
timestamp ×2
annotations ×1
date ×1
datetime ×1
java-8 ×1
java-stream ×1
python ×1
spock ×1
spring ×1
spring-boot ×1