我是单元测试的新手,并使用JUnit测试在Grails应用程序中进行一些测试。我使用的是grails-2.3.7。我搜索了错误,但无法得到它。当我测试我的测试用例时,我得到以下错误:
java.lang.NullPointerException: Cannot get property 'mainContext' on null object
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
我正在测试资源。我的测试代码是:
import com.vox.client.portal.DeliveryStatus
import com.vox.client.portal.OrderDetails
import com.vox.client.portal.testing.AbstractResourceTests
import groovy.json.JsonBuilder
import org.junit.Before
import org.junit.Test
class OrderResourceTests extends AbstractResourceTests {
JsonBuilder json;
@Before
void setup() {
super.setUp()
json = new JsonBuilder ()
}
@Test
void "test creating an order"() {
def order = json {
user "vox"
password "vox"
order(
orderDate: "2015.12.25",
orderNumber: "10",
subTotal: "123",
shipping: "21",
discount: "2.2",
netTotalPaid: "5",
creditApplied: "true",
transactionId: "1",
specialInstruction: "abc",
deliveryStatus: "DELIVERED",
memberId: "1",
localOffice: "Bye Bye"
)
}
sendPost('/api/v1/order/create', order)
assertResponseStatus(200)
}
}
继承类:
import grails.converters.JSON
import org.grails.jaxrs.itest.IntegrationTestCase
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertTrue
class AbstractResourceTests extends IntegrationTestCase{
def parsedData
void send(String method, String url, def data) {
def headers = ['Content-Type': 'application/json', 'Accept': 'application/json']
def conStr = (data as JSON)?.toString()
sendRequest url, method, headers, conStr.bytes
}
void sendPost(String url, def data) {
send 'POST', url, data
}
def assertResponseStatus(int httpStatus) {
assertEquals httpStatus, response.status
if (response.text) {
assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
parsedData = JSON.parse response.text
assertNotNull parsedData
} else {
parsedData = null
}
return parsedData
}
}
我的BuildConfig.groovy是:
grails.servlet.version = "3.0" // Change depending on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.work.dir = "target/work"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"
grails.project.fork = [
// configure settings for compilation JVM, note that if you alter the Groovy version forked compilation is required
// compile: [maxMemory: 256, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
// configure settings for the test-app JVM, uses the daemon by default
test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
// configure settings for the run-app JVM
run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
// configure settings for the run-war JVM
war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
// configure settings for the Console UI JVM
console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]
grails.project.dependency.resolver = "maven" // or ivy
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// specify dependency exclusions here; for example, uncomment this to disable ehcache:
// excludes 'ehcache'
}
log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility
repositories {
inherits true // Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
mavenLocal()
grailsCentral()
mavenCentral()
mavenRepo 'http://maven.restlet.org/'
}
dependencies {
runtime 'mysql:mysql-connector-java:5.1.29'
test "org.gebish:geb-spock:0.12.2"
provided 'org.spockframework:spock-grails-support:0.5-groovy-1.7', {
exclude 'groovy-all'
}
}
plugins {
// plugins for the build system only
build ":tomcat:7.0.52.1"
// plugins for the compile step
compile ":scaffolding:2.0.2"
compile ':cache:1.1.1'
compile ':spring-security-core:2.0-RC5'
// plugins needed at runtime but not for compilation
runtime ":hibernate:3.6.10.9" // or ":hibernate4:4.3.4"
runtime ":database-migration:1.3.8"
runtime ":jquery:1.11.0.2"
runtime ":resources:1.2.7"
compile ':jaxrs:0.10'
compile ":rest-client-builder:2.1.1"
test("org.grails.plugins:geb:0.12.2")
compile ':functional-spock:0.6'
test ":spock:0.7"
}
}