Write Spock test cases for Spring boot application [closed]
Asked Answered
H

2

6

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"?

Humpback answered 17/6, 2015 at 8:3 Comment(1)
Yes, you can write such tests. However this question will be closed probably as off topic. You need to ask more detailed questions.Elveraelves
C
9

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

Cloud answered 17/6, 2015 at 8:11 Comment(2)
thanks for nice explanation, i saw at everywhere it uses for groovy syntex like def "adder-test"() { given: "a new Adder class is created" def adder = new Adder(); expect: "Adding two numbers to return the sum" adder.add(3, 4) == 7 } but i didn't want to use,can i do this without using def etcHumpback
@PrabjotSingh You will be using Groovy if you want to work with Spock, but that's ok because the Spring framework supports Groovy, and Groovy can be freely mixed with Java. A good addition to your knowledge portfolioCloud
S
2

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
Siegbahn answered 7/11, 2017 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.