Grails 3.3.8 单元测试服务
Grails 3.3.8 unit testing service
我正在尝试测试使用 src/main/webapp/folder 下的文件的服务方法,但是它不起作用,出于某种原因它似乎无法访问或其他原因,获取文件的代码是以下:
def file = grailsApplication.parentContext.getResource("folder/file.txt").file
错误是:
ServletContext resource [/folder/file.txt] cannot be resolved to absolute file path - web application archive not expanded?
有什么想法吗?
它不是 grailsApplication inyection 或 get 资源方法我认为当测试框架使用该服务时,该服务无法访问该路径。
在 https://github.com/jeffbrown/davidpadillaservice 查看项目。
https://github.com/jeffbrown/davidpadillaservice/blob/master/src/main/resources/folder/file.txt
line number one
line number two
line number three
package davidpadillaservice
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
class SomeResourceService {
@Value("classpath:folder/file.txt")
Resource fileResource
String getFirstLine() {
getLine(0)
}
String getSecondLine() {
getLine(1)
}
protected String getLine(int index) {
fileResource.file.readLines().get(index)
}
}
package davidpadillaservice
import grails.testing.services.ServiceUnitTest
import spock.lang.Specification
class SomeResourceServiceSpec extends Specification implements ServiceUnitTest<SomeResourceService>{
void "test interacting with the service"() {
expect:
service.firstLine == 'line number one'
service.secondLine == 'line number two'
}
}
希望对您有所帮助。
我正在尝试测试使用 src/main/webapp/folder 下的文件的服务方法,但是它不起作用,出于某种原因它似乎无法访问或其他原因,获取文件的代码是以下:
def file = grailsApplication.parentContext.getResource("folder/file.txt").file
错误是:
ServletContext resource [/folder/file.txt] cannot be resolved to absolute file path - web application archive not expanded?
有什么想法吗? 它不是 grailsApplication inyection 或 get 资源方法我认为当测试框架使用该服务时,该服务无法访问该路径。
在 https://github.com/jeffbrown/davidpadillaservice 查看项目。
https://github.com/jeffbrown/davidpadillaservice/blob/master/src/main/resources/folder/file.txt
line number one
line number two
line number three
package davidpadillaservice
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
class SomeResourceService {
@Value("classpath:folder/file.txt")
Resource fileResource
String getFirstLine() {
getLine(0)
}
String getSecondLine() {
getLine(1)
}
protected String getLine(int index) {
fileResource.file.readLines().get(index)
}
}
package davidpadillaservice
import grails.testing.services.ServiceUnitTest
import spock.lang.Specification
class SomeResourceServiceSpec extends Specification implements ServiceUnitTest<SomeResourceService>{
void "test interacting with the service"() {
expect:
service.firstLine == 'line number one'
service.secondLine == 'line number two'
}
}
希望对您有所帮助。