Spring Boot-在没有Spring上下文的情况下使用Spock测试

时间:2019-03-04 18:39:42

标签: spring-boot mocking spock

我有一个带有签名的服务类和方法:

@Service
public class FileService {
    ....
    ....
    public Optional<FileDescr> upload(MultipartFile uploadFile){...}

    public Resource downloadFile(int linkID){...}

}

和测试文件(Groovy):

import org.junit.experimental.categories.Category
import org.springframework.mock.web.MockMultipartFile
import spock.lang.Shared
import spock.lang.Specification

@Category(WithoutSpringContext)
class FileServiceTestWithoutSpringContext extends Specification{

    @Shared
    FileService fileService = Mock()

    @Shared
    MockMultipartFile mockMultipartFile = Mock()

    def setupSpec(){

    }

    def setup(){

    }

    def clean(){

    }

    def cleanSpec(){

    }

    def "upload file"(){
        given:
            fileService.upload(mockMultipartFile) >> true
        when:
            def result = fileService.upload(mockMultipartFile)
        then:
            result==true
    }

    def "download file if exists"(){
        given:
            fileService.downloadFile(1) >> true
        when:
            def result = fileService.downloadFile(1)
        then:
            result==true
    }
}

我想在没有Spring上下文的情况下使用Mock测试方法。我该怎么办?现在,result变量返回null。 我想设置方法的返回值。

0 个答案:

没有答案
相关问题