Spock - return fixed value does not work as expected

I have to start with an apology if the terms that I use are incorrect. I use groovy / java only for automation tasks (Gradle), and I do not have many years of experience providing software for production.

So, the problem that I have is the following: I have a specification that is trying to check the returned string as expected (almost identical to this ).

def "pretty print returns correct string"() { setup: X509Certificate stubCert = Stub() stubCert.getSubjectDN().toString() >> "Test" when: def output = X509CertificateUtils.prettyPrint(stubCert) then: output == "Subject: Test" } 

However, the difference is that my method restriction returns a Principal object and its toString () object, which I really want to stub. I thought I was doing it right above, but it does not give the expected result.

Here is my helper class.

 import java.security.cert.X509Certificate class X509CertificateUtils { static def prettyPrint(X509Certificate x509Certificate) { return "Subject: ${x509Certificate.getSubjectDN()}" } } 

If I run this test, I get the following error:

 output == "Subject: Test" | | | false | 38 differences (20% similarity) | Subject: (Mock for )t(ype 'Principal' named 'dummy') | Subject: (Tes------)t(-----------------------------) Subject: Mock for type 'Principal' named 'dummy' 

Any help would be greatly appreciated.

+5
source share
2 answers

Just create a second stub:

 X509Certificate stubCert = Stub() Principal princ = Stub() princ.toString() >> "Test" stubCert.getSubjectDN() >> princ 
+6
source

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}" } 
+3
source

All Articles