検証を成功させようとしている単体テストがあります。
基本的に、 を呼び出すとbadMockedSecureFile.validate()
、期待どおりに動作せず、2 つのフィールドと の検証に失敗していencryptedFileName
ますencryptedFileData
。
デバッガーを中断すると、後続のアサートでnull
値を取得するだけです。badMockedSecureFile.errors
ここに私の2つのファイルがあります:
任意の入力をいただければ幸いです。まったく同じ質問が見つかりませんでした。問題があれば、Oracle JDK 1.7.0_25でgrails 2.2.4を使用しています。
編集mockForConstraintTests
:私は呼び出しを削除したことに注意したいだけで、現在は機能しているようです。どこかで RTFM をしなかったような気がしますが、この動作は単体テストで変更されましたか、それとも何か他のことが起こっていますか?
SecureFile.groovy
class SecureFile implements Serializable {
/**
* An unencrypted version of the file name. This file name is unencrypted
* when the appropriate password and key combo is used and it is never
* persisted to the database for security (see transients below).
*/
String fileName
/**
* Unencrypted version of the file data. Never persisted to the
* database for security (see transients below).
*/
byte[] fileData
String encryptedFileName
byte[] encryptedFileData
Date dateAdded
Date dateUpdated
Date dateDeleted
static constraints = {
encryptedFileName(nullable: false, blank: false)
encryptedFileData(nullable: false)
}
static transients = ["fileName", "fileData"]
static belongsTo = [user: User]
}
SecureFileTests.groovy
import static org.junit.Assert.*
import grails.test.mixin.*
import grails.test.mixin.support.*
import org.junit.*
/**
* See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
*/
@TestFor(SecureFile)
class SecureFileTests {
static final String SAMPLE_PDF_FILE = "fileEncryptionTestSample.pdf"
void testConstraints() {
def samplePdfFile = new FileInputStream(SAMPLE_PDF_FILE)
// Not really encrypted for this mock.
def mockedSecureFile = new SecureFile(
encryptedFileName: "--Not-Really-Encrypted--",
encryptedFileData: samplePdfFile.getBytes()
)
mockForConstraintsTests(SecureFile, [mockedSecureFile])
// Validation should fail if both properties are null.
def badMockedSecureFile = new SecureFile()
assert !badMockedSecureFile.validate()
assert "nullable" == badMockedSecureFile.errors["encryptedFileName"].code
assert "nullable" == badMockedSecureFile.errors["encryptedFileData"].code
}
}