Spock has several approaches to faking objects. Here is the current documentation .
- Stub : a fake object that returns only what it is told; the default value is otherwise (0, Empty, etc.).
- Mock . Like a stub, but it can also check the number of method calls made on an object.
- Spy : the normal creation of your object, mocks are used as listeners that intercept calls to the object. This allows you to use the object in normal mode only by changing the behavior of the specified methods. You can also call the source code at some point during your bullying.
My question is for you ... Are you trying to verify that prettyPrint () is working correctly, that SubjectDN.toString () is printing correctly, or a combination of the two? My suggestion is for your layout to return the actual SubjectDN () object that you then tested. Not much more work, and if something breaks, you have a better idea of โโwhere the problem arose. The maximum solution will solve your question; I have not read close enough or stuck with good validation methods. I will leave the rest of my answer as food for thought. If you want to associate Max stub with my parameterization, I would suggest passing the desired line in the where block to create a stub in the configuration block.
This started to move away from the topic, but if you need to test more than one SubjectDN script (empty, empty, various capital letters, numbers, etc.); You should also study the parameterization of your test.
def "pretty print returns correct string"() { setup: X509Certificate stubCert = Mock() stubCert.getSubjectDN() >> subject expect: subject.toString() == expectedToString X509CertificateUtils.prettyPrint(stubCert) == expectedFormatted where: subject | expectedToString | expectedFormatted new SubjectDN("") | ???? | "Subject: ????" new SubjectDN(null) | ???? | "Subject: ????" new SubjectDN("test") | "test" | "Subject: Test" new SubjectDN("testing") | "testing" | "Subject: Testing" }
If your pretty print is really as simple as adding โSubject:โ, you will probably be able to calculate your expected variable; but you really shouldn't test your test for testable code, trying to make testing easier.
I also found that the format of the test parameter table becomes erratic or difficult to maintain when iterations have a fluid length. My preference is to make a list of maps, with each map representing a test iteration. It keeps every test iteration complete and gives unfamiliar developers a better idea of โโwhat each iteration of the test entails.
@Unroll(iteration.testName) // Shows each iteration in its own result (in most IDEs) def "testing printing of a SubjectDN"() { setup: X509Certificate stubCert = Mock() stubCert.getSubjectDN() >> iteration.subject expect: subject.toString() == iteration.expectedToString X509CertificateUtils.prettyPrint(stubCert) == expectedFormatted where: iteration << [ [testName: "Testing pretty printing a normal SubjectDN", subject: new SubjectDN("test"), expectedToString: "test"], [testName: "Testing pretty printing a normal SubjectDN", subject: new SubjectDN("testing 123"), expectedToString: "testing 123"], [testName: "Testing pretty printing a null SubjectDN", subject: new SubjectDN(null), expectedToString: ????] // And so on... ] expectedFormatted = "Subject: ${iteration.expectedToString}" }