Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Spock test cases for Spring boot application [closed]

I am working on spring boot application. I have to write test cases for it. I haven't written test cases before, so someone suggested using spock framework for it. I explored spock and i think it is more related to groovy language.

Can i write spock test cases for my spring application?

If so then could you suggest me a better documentation of "how to use it with spring boot application"?

like image 425
Prabjot Singh Avatar asked Jun 17 '15 08:06

Prabjot Singh


2 Answers

Yes, you can write spock test cases for your spring application.

Look at the official documentation for an example of Spock testing with Spring Boot

35.3.1 Using Spock to test Spring Boot applications

A simple google search reveals a basic example of Using Spock to test Spring classes.

Spock relies heavily on the Spring's TestContext framework and does this via the @ContextConfiguration annotation. This allows the test specification class to load an application context from one or more locations.

Spring One g2x has a large presentation on Testing Java, Groovy, Spring and Web Applications with Spock.

Groovy and Java can freely be mixed together you can use any Java based library you like, or use Groovy based libraries.

The Spring framework supports Groovy and - not surprisingly - Spring TestContext framework works well with Spock Spock specifications can be run from an IDE just like normal JUnit tests and, last but not least, implementing them is a great opportunity to learn the Groovy language.source

like image 190
Laurentiu L. Avatar answered Oct 22 '22 20:10

Laurentiu L.


Below are the steps which would help you to write spock test using Spring boot.

@ContextConfiguration(classes = StartApplication.class, loader =     SpringApplicationContextLoader.class)
@TestPropertySource(locations = "classpath:application-iTest.properties")
@WebAppConfiguration
@ComponentScan( "com.xyz")
@ActiveProfiles("iTest")
@IntegrationTest("server.port:0")
class TestAuditReport extends Specification{

   def Test1(){
    given :
    when:
    then:
    1==1
}

}
  • @ContextConfiguration(classes = StartApplication.class, loader = SpringApplicationContextLoader.class) // StartApplication.Java is main class which run Spring boot Application.
  • @TestPropertySource(locations = "classpath:application-iTest.properties") //
  • @WebAppConfiguration // if web applicaiton is configured in your applicaiton.
  • @ComponentScan("com.xyx") // package name to be scanned.
  • @ActiveProfiles("iTest") // Defined profile name i.e iTest therefore a file name "filePrefix-iTest.properties will be look up in /test/java/resource folder"
  • @IntegrationTest("server.port:0") // It would run any port number, again this annotation required only when @WebAppConfiguration enabled. class TestAuditReport extends Specification
like image 33
Ajay Kumar Avatar answered Oct 22 '22 20:10

Ajay Kumar