参考:https://semaphoreci.com/community/tutorials/stubbing-and-mocking-in-java-with-the-spock-testing-framework
了解groovy
Groovy 是一种基于 JVM 的敏捷开发语言,它结合了 Python、Ruby 和 Smalltalk 的许多强大的特性,Groovy 代码能够与 Java 代码很好地结合,也能用于扩展现有代码。由于其运行在 JVM 上的特性,Groovy 可以使用其他 Java 语言编写的库。
另请注意,任何语句的行尾都没有分号。与 Java 不同,Groovy 不需要分号。
1:引入的包
grandle方式
//1 使用插件,引用 groovy 要不会 不识别的
plugins {
id 'io.jmix' version '1.0.1'
id 'java'
id 'groovy'
}
//2 引用spck包
testImplementation "org.spockframework:spock-core"
testImplementation "org.spockframework:spock-spring"
2:了解语法
given: “a customer with example name values”
and: “an entity manager that always returns this customer”
and: “a customer reader which is the class under test”
when: “we ask for the full name of the customer”
then: “we get both the first and the last name”
3:新建groovy文件
4:service方法
这里 一定要在service 里面 用构造注入的方式,一定不要用@Autowried 方式,否则groovy测试的时候找不到的
@Service(RemoteAgentService.NAME)
public class RemoteAgentServiceBean implements RemoteAgentService {
private final AgentService agentService;
public RemoteAgentServiceBean(AgentService agentService) {
this.agentService = agentService;
}
@Override
@Authenticated
public Agent register(String name, UUID agentId, String host, String version, String ipAddress) {
if(!validateParams(name,agentId,host,version,ipAddress)){
return null;
}else{
return Optional.ofNullable(agentService.getAgentByExternalId(agentId, AGENT_CHECK_VIEW))
.orElseGet(() -> agentService.createAgent(name, agentId, host, version, ipAddress));
}
}
}
具体测试类
方式1:
class RemoteAgentServiceTest extends Specification {
def "Test"() {
def now = "1111111231231"
println now
expect:
assertThat(now).isNotNull()
assertThat(now).isEqualTo("1111111231231")
}
AgentService agentService
def serviceBean
void setup(){
agentService= Mock(AgentService.class)
serviceBean = new RemoteAgentServiceBean(agentService)
}
def "register"() {
given:
def name = "test2"
def agentId = UUID.fromString("1235922b-f697-47db-b237-2078b090c43a")
def host = ""
def version = "1.0.0"
def ipAddress = "172.17.0.12"
def view = "agent-check-view"
and:
agentService.getAgentByExternalId(agentId, view) >> null
when:
agentService.createAgent(name, agentId, host, version, ipAddress) >> new Agent(name:name)
def agent = serviceBean.register(name, agentId, host, version, ipAddress)
then:
agent.name=="test2"
}
}
方式2:
class RemoteAgentServiceTest extends Specification {
AgentService agentService
RemoteAgentServiceBean serviceBean
static UUID randomUUID = UUID.randomUUID()
static UUID uuid = UUID.fromString("1235922b-f697-47db-b237-2078b090c43a")
static String view = "agent-check-view"
@SuppressWarnings(['GrUnresolvedAccess', 'unused'])
def setup() {
agentService = Stub {
getAgentByExternalId(uuid, view) >> new Agent(name: "registered agent")
getAgentByExternalId(randomUUID, view) >> null
}
serviceBean = new RemoteAgentServiceBean(agentService)
}
def "register"(String name, UUID externalId) {
given: "定义入参"
def host = ""
def version = "1.0.0"
def ipAddress = "172.17.0.12"
and: "模拟创建agent方法"
agentService.createAgent(name, externalId, host, version, ipAddress) >> new Agent(name: name)
expect: "注册agent"
def agent = serviceBean.register(name, externalId, host, version, ipAddress)
result == agent.getName()
where: "测试"
name | externalId || result
"test1" | uuid || "registered agent"
"test2" | randomUUID || "test2"
}
}
|