作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题是:我是Spock测试的新手,并试图在此User Class上获得100%的代码覆盖率。首先,有人可以帮我弄清楚如何测试构造函数。我目前没有使用cobertura插件来覆盖它。另外,如果有人对Spock + Cobertura有所了解,也许您可以阐明我做错了什么,以及一些进一步测试的指示。
我有一个代表用户的类:
import java.io.Serializable;
import java.util.Set;
class User implements Serializable {
String email
byte[] photo
static hasMany = [lineups: Lineup]
private static final long serialVersionUID = 1
transient springSecurityService
String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
User(String username, String password) {
this()
this.username = username
this.password = password
}
@Override
int hashCode() {
username?.hashCode() ?: 0
}
@Override
boolean equals(other) {
is(other) || (other instanceof User && other.username == username)
}
Set<SecRole> getAuthorities() {
SecUserSecRole.findAllBySecUser(this)*.secRole
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
static transients = ['springSecurityService']
static constraints = {
username blank: false, unique: true
password blank: false
email(unique: true, blank: false, nullable: true) // needs to be moved to account
photo(nullable:true, maxSize: 1024 * 1024 * 2 /* 2MB */)
}
static mapping = {
password column: '`password`'
}
String toString() {
return id + ": " + email + " " + username
}
}
@TestFor(User)
class UserSpec extends Specification {
User user
def setup() {
user = new User(
username: "fredflintstone",
password: "Wilma1",
enabled: true).save(failOnError: true, flush: true)
}
def cleanup() {
user = null
}
// Constructor tests
void "test to check constructor works"() {
when:
mockForConstraintsTests(User, [user])
expect:
user.validate()
user != null
user.username != null
user.password != null
}
void "test #hashCode works"() {
setup:
mockForConstraintsTests(User, [user])
expect:
user.username.hashCode() != null
}
}
最佳答案
第一
您真的不需要(也不应该浪费时间)测试仅在JVM中的某些内容中断时才能中断的代码。测试基本的getter和setter方法不会使您的代码受益,因为逻辑太简单了,难以破解。因此,100%的测试覆盖率是一个糟糕的测试目标。您应该瞄准的是...
@Unroll(iteration.testName)
void "testing the constructor"() {
setup:
user = new User(
username: iteration.username,
password: iteration.password,
enabled: iteration.enabled).save(failOnError: true, flush: true)
when:
mockForConstraintsTests(User, [user])
expect:
user != null
user.validate() == iteration.valid
user.username == iteration.username
user.password == iteration.password
user.enabled == iteration.enabled
user.username.hashCode() != null // May need modification
where:
iteration << [
[testName: "Testing a normal enabled object"
username: "fredflintstone"
password: "Wilma1"
enabled: true,
valid: true ],
[testName: "Testing a normal disabled object"
username: "fredflintstone"
password: "Wilma1"
enabled: false,
valid: true ],
[testName: "Testing a disabled user, null name"
username: null
password: "Wilma1"
enabled: false,
valid: false ],
[testName: "Testing a disabled user, empty name"
username: ""
password: "Wilma1"
enabled: false,
valid: false ],
]
}
关于unit-testing - 如何在Spock单元测试中测试Groovy构造函数并覆盖100%的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32402084/
我是一名优秀的程序员,十分优秀!