// class under specification
public class TeamService {
// method under specification
public void deleteTeam(String id) {
/* some other calls */
this.moveAssets(team) // calls method within the class under spec.
}
// I would like to stub / mock this method
public void moveAssets(Team team){
// logic
}
}
斯波克规格
def deleteTeam(){
given:
TeamService teamService = new TeamService()
when: 'I delete the team'
teamService.deleteTeam()
then: 'I want to check that moveAssets gets called(team resources going to be preserved
and that deleteTeam (external class that deletes the team) gets called (has no issues here)'
1 * teamService.moveAssets(id) >> {} //See Q1
/*
Q1: I want to test that this call is made
but currently it does not count as interaction for some reason so I get an error.
From what I read you can't stub the calls unless the class is mocked -
but here I do have an important need to check on the method within the class under specification
(that is not mocked). What are my options?
I know I can in theory move moveAssets to some other class,
which I can then mock and accomplish it but that does not feel right.
*/
}
如何测试从“Specification下的方法(teamservices.deleteteam())”调用的“Specification下的类”teamservices.moveAssets())方法?
https://github.com/spockframework/spock/discussions/1346
正如您已经注意到的,您只能检查模拟对象类型上的交互,即mock、stub或spy。后者,间谍,是您在这种情况下所需要的,即类似于:
TeamService teamService =
Spy(constructorArgs: [mockedInjectedService1, mockedInjectedService2])
下面是一个MCVE:
package de.scrum_master.stackoverflow.q67950174
class Team {
String id
}
package de.scrum_master.stackoverflow.q67950174
class TeamService {
def dep1, dep2
TeamService(dep1, dep2) {
this.dep1 = dep1
this.dep2 = dep2
}
boolean moveAssets(Team team) {
println "Moving assets for team $team"
return false
}
void deleteTeam(String id) {
Team team = findTeamById(id)
moveAssets(team)
delete(team)
}
Team findTeamById(String id) {
println "Searching for team $team"
return new Team(id: id)
}
void delete(Team team) {
println "Deleting team $team"
}
}
package de.scrum_master.stackoverflow.q67950174
import spock.lang.Specification
class TeamServiceTest extends Specification {
def deleteTeam() {
given:
Team teamDocument = new Team(id: "dummy")
String id = "foo"
def mockedInjectedService1 = "X"
def mockedInjectedService2 = "Y"
TeamService teamService = Spy(constructorArgs: [mockedInjectedService1, mockedInjectedService2])
when: 'I delete the team'
teamService.deleteTeam(id)
then: 'I want to check that moveAssets gets called(team resources going to be preserved'
1 * teamService.findTeamById(id) >> teamDocument
1 * teamService.moveAssets(teamDocument) >> true
}
}
我需要测试验证注释,但看起来它们不起作用。我不确定JUnit是否也是正确的。目前,测试将通过,但您可以看到指定的电子邮件地址是错误的。 JUnit 待测试类别
我有一个“AllowedValuesValidator.java”类: 以及相应的接口类: 我希望能够编写一个单元测试类来测试验证器中的直接逻辑。但我在谷歌上搜索的大多数地方似乎都给出了测试类的例子,我们基本上测试了给定模型类的所有验证器,例如: 我不想构建模拟模型来测试所有验证器。有没有一种方法可以创建一个单独的测试类,只在一个验证器中直接测试逻辑,而不使用任何其他模型类等?
我需要对使用WebClient的类进行单元测试。有什么好方法来处理网络客户端吗?有了RestTemplate,我可以很容易地使用Mockito。模拟WebClient有点乏味,因为深度存根不适用于WebClient。。。 我想测试我的代码是否提供了正确的头。。。缩短的样本代码:
我刚刚开始对我的系统采用Pact测试,它由一个提供者服务和一个作为消费者的棱角前端组成。我成功地设置了双方,因此,Angular应用程序生成了一个(单个)pact文件,与我的提供者服务的多个endpoint进行了许多交互。在提供程序中,我现在确实面临这样一个问题,即我的验证测试变得非常庞大和过于复杂,因为我必须在一个测试中用所有endpoint的数据模拟所有endpoint,例如: 有没有一种方法
当与应用程序相关的身份验证功能未正确实现时,它允许黑客利用其他用户凭据破坏密码或会话ID或利用其他实施缺陷。 下面我们借助简单的图表来了解这个漏洞的威胁代理,攻击向量,安全弱点,技术影响和业务影响。 威胁代理 - 匿名外部攻击者以及拥有自己帐户的用户可能会尝试从其他人那里窃取帐户。 攻击者的方法 - 在身份验证或会话管理功能中使用泄漏或漏洞。例如,公开的帐户密码,用于冒充用户的会话ID。 安全弱点