mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 18:22:26 +08:00
Add support for new test infrastructure
Spring Boot `1.4.0.M2` brings a new test infrastructure with nice defaults and deprecates `SpringBootConfiguration` in the process. This commits automatically upgrades generated project to this new infrastructure if Spring Boot `1.4.0.M2` or later is available. Closes gh-215
This commit is contained in:
@@ -44,6 +44,8 @@ class ProjectGenerator {
|
|||||||
|
|
||||||
private static final VERSION_1_3_0_M1 = Version.parse('1.3.0.M1')
|
private static final VERSION_1_3_0_M1 = Version.parse('1.3.0.M1')
|
||||||
|
|
||||||
|
private static final VERSION_1_4_0_M2 = Version.parse('1.4.0.M2')
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
ApplicationEventPublisher eventPublisher
|
ApplicationEventPublisher eventPublisher
|
||||||
|
|
||||||
@@ -138,13 +140,7 @@ class ProjectGenerator {
|
|||||||
|
|
||||||
def test = new File(new File(dir, "src/test/$codeLocation"), request.packageName.replace('.', '/'))
|
def test = new File(new File(dir, "src/test/$codeLocation"), request.packageName.replace('.', '/'))
|
||||||
test.mkdirs()
|
test.mkdirs()
|
||||||
if (request.hasWebFacet()) {
|
setupTestModel(request, model)
|
||||||
model.testAnnotations = '@WebAppConfiguration\n'
|
|
||||||
model.testImports = 'import org.springframework.test.context.web.WebAppConfiguration;\n'
|
|
||||||
} else {
|
|
||||||
model.testAnnotations = ''
|
|
||||||
model.testImports = ''
|
|
||||||
}
|
|
||||||
write(new File(test, "${applicationName}Tests.${extension}"), "ApplicationTests.$extension", model)
|
write(new File(test, "${applicationName}Tests.${extension}"), "ApplicationTests.$extension", model)
|
||||||
|
|
||||||
def resources = new File(dir, 'src/main/resources')
|
def resources = new File(dir, 'src/main/resources')
|
||||||
@@ -228,9 +224,46 @@ class ProjectGenerator {
|
|||||||
model['bootOneThreeAvailable'] = VERSION_1_3_0_M1
|
model['bootOneThreeAvailable'] = VERSION_1_3_0_M1
|
||||||
.compareTo(Version.safeParse(request.bootVersion)) <= 0
|
.compareTo(Version.safeParse(request.bootVersion)) <= 0
|
||||||
|
|
||||||
|
// New testing stuff
|
||||||
|
model['newTestInfrastructure'] = isNewTestInfrastructureAvailable(request)
|
||||||
|
|
||||||
model
|
model
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setupTestModel(ProjectRequest request, Map<String, Object> model) {
|
||||||
|
String imports = ''
|
||||||
|
String testAnnotations = ''
|
||||||
|
def newTestInfrastructure = isNewTestInfrastructureAvailable(request)
|
||||||
|
if (newTestInfrastructure) {
|
||||||
|
imports += String.format(generateImport('org.springframework.boot.test.context.SpringBootTest',
|
||||||
|
request.language) + "%n")
|
||||||
|
imports += String.format(generateImport('org.springframework.test.context.junit4.SpringRunner',
|
||||||
|
request.language) + "%n")
|
||||||
|
} else {
|
||||||
|
imports += String.format(generateImport('org.springframework.boot.test.SpringApplicationConfiguration',
|
||||||
|
request.language) + "%n")
|
||||||
|
imports += String.format(generateImport('org.springframework.test.context.junit4.SpringJUnit4ClassRunner',
|
||||||
|
request.language) + "%n")
|
||||||
|
}
|
||||||
|
if (request.hasWebFacet() && !newTestInfrastructure) {
|
||||||
|
imports += String.format(generateImport('org.springframework.test.context.web.WebAppConfiguration',
|
||||||
|
request.language) + "%n")
|
||||||
|
testAnnotations = String.format('@WebAppConfiguration%n')
|
||||||
|
}
|
||||||
|
model.testImports = imports
|
||||||
|
model.testAnnotations = testAnnotations
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String generateImport(String type, String language) {
|
||||||
|
String end = (language.equals("groovy") || language.equals("kotlin")) ? '' : ';'
|
||||||
|
"import $type$end"
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isNewTestInfrastructureAvailable(ProjectRequest request) {
|
||||||
|
VERSION_1_4_0_M2
|
||||||
|
.compareTo(Version.safeParse(request.bootVersion)) <= 0
|
||||||
|
}
|
||||||
|
|
||||||
private byte[] doGenerateMavenPom(Map model) {
|
private byte[] doGenerateMavenPom(Map model) {
|
||||||
template 'starter-pom.xml', model
|
template 'starter-pom.xml', model
|
||||||
}
|
}
|
||||||
|
@@ -2,11 +2,11 @@ package ${packageName}
|
|||||||
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
${testImports}import org.springframework.boot.test.SpringApplicationConfiguration
|
${testImports}<% if (newTestInfrastructure) { %>
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
@RunWith(SpringRunner)
|
||||||
|
@SpringBootTest<% } else { %>
|
||||||
@RunWith(SpringJUnit4ClassRunner)
|
@RunWith(SpringJUnit4ClassRunner)
|
||||||
@SpringApplicationConfiguration(classes = ${applicationName})
|
@SpringApplicationConfiguration(classes = ${applicationName})<% } %>
|
||||||
${testAnnotations}class ${applicationName}Tests {
|
${testAnnotations}class ${applicationName}Tests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -2,11 +2,11 @@ package ${packageName};
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
${testImports}import org.springframework.boot.test.SpringApplicationConfiguration;
|
${testImports}<% if (newTestInfrastructure) { %>
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest<% } else { %>
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = ${applicationName}.class)
|
@SpringApplicationConfiguration(classes = ${applicationName}.class)<% } %>
|
||||||
${testAnnotations}public class ${applicationName}Tests {
|
${testAnnotations}public class ${applicationName}Tests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -2,11 +2,11 @@ package ${packageName}
|
|||||||
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
${testImports}import org.springframework.boot.test.SpringApplicationConfiguration
|
${testImports}<% if (newTestInfrastructure) { %>
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
@RunWith(SpringRunner::class)
|
||||||
|
@SpringBootTest<% } else { %>
|
||||||
@RunWith(SpringJUnit4ClassRunner::class)
|
@RunWith(SpringJUnit4ClassRunner::class)
|
||||||
@SpringApplicationConfiguration(classes = arrayOf(${applicationName}::class))
|
@SpringApplicationConfiguration(classes = arrayOf(${applicationName}::class))<% } %>
|
||||||
${testAnnotations}class ${applicationName}Tests {
|
${testAnnotations}class ${applicationName}Tests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package io.spring.initializr.generator
|
package io.spring.initializr.generator
|
||||||
|
|
||||||
|
import io.spring.initializr.metadata.Dependency
|
||||||
import io.spring.initializr.metadata.InitializrMetadata
|
import io.spring.initializr.metadata.InitializrMetadata
|
||||||
import io.spring.initializr.metadata.SimpleInitializrMetadataProvider
|
import io.spring.initializr.metadata.SimpleInitializrMetadataProvider
|
||||||
import io.spring.initializr.test.generator.GradleBuildAssert
|
import io.spring.initializr.test.generator.GradleBuildAssert
|
||||||
@@ -49,8 +50,11 @@ abstract class AbstractProjectGeneratorTests {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
void setup() {
|
void setup() {
|
||||||
|
def web = new Dependency(id: 'web')
|
||||||
|
web.facets << 'web'
|
||||||
def metadata = InitializrMetadataTestBuilder.withDefaults()
|
def metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||||
.addDependencyGroup('test', 'web', 'security', 'data-jpa', 'aop', 'batch', 'integration').build()
|
.addDependencyGroup('web', web)
|
||||||
|
.addDependencyGroup('test', 'security', 'data-jpa', 'aop', 'batch', 'integration').build()
|
||||||
applyMetadata(metadata)
|
applyMetadata(metadata)
|
||||||
projectGenerator.eventPublisher = eventPublisher
|
projectGenerator.eventPublisher = eventPublisher
|
||||||
projectGenerator.tmpdir = folder.newFolder().absolutePath
|
projectGenerator.tmpdir = folder.newFolder().absolutePath
|
||||||
|
@@ -89,6 +89,16 @@ class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests {
|
|||||||
.equalsTo(new ClassPathResource("project/$language/standard/DemoApplicationTests.$expectedExtension"))
|
.equalsTo(new ClassPathResource("project/$language/standard/DemoApplicationTests.$expectedExtension"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void standardTestClassWeb() {
|
||||||
|
def request = createProjectRequest('web')
|
||||||
|
request.language = language
|
||||||
|
|
||||||
|
def project = generateProject(request)
|
||||||
|
project.sourceCodeAssert("src/test/$language/com/example/DemoApplicationTests.$extension")
|
||||||
|
.equalsTo(new ClassPathResource("project/$language/standard/DemoApplicationTestsWeb.$expectedExtension"))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void standardServletInitializer() {
|
public void standardServletInitializer() {
|
||||||
def request = createProjectRequest()
|
def request = createProjectRequest()
|
||||||
@@ -99,4 +109,26 @@ class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests {
|
|||||||
.equalsTo(new ClassPathResource("project/$language/standard/ServletInitializer.$expectedExtension"))
|
.equalsTo(new ClassPathResource("project/$language/standard/ServletInitializer.$expectedExtension"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test14TestClass() {
|
||||||
|
def request = createProjectRequest()
|
||||||
|
request.language = language
|
||||||
|
request.bootVersion = '1.4.0.M2'
|
||||||
|
|
||||||
|
def project = generateProject(request)
|
||||||
|
project.sourceCodeAssert("src/test/$language/com/example/DemoApplicationTests.$extension")
|
||||||
|
.equalsTo(new ClassPathResource("project/$language/test-1.4/DemoApplicationTests.$expectedExtension"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test14TestClassWeb() {
|
||||||
|
def request = createProjectRequest('web')
|
||||||
|
request.language = language
|
||||||
|
request.bootVersion = '1.4.0.M2'
|
||||||
|
|
||||||
|
def project = generateProject(request)
|
||||||
|
project.sourceCodeAssert("src/test/$language/com/example/DemoApplicationTests.$extension")
|
||||||
|
.equalsTo(new ClassPathResource("project/$language/test-1.4/DemoApplicationTests.$expectedExtension"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner)
|
||||||
|
@SpringApplicationConfiguration(classes = DemoApplication)
|
||||||
|
@WebAppConfiguration
|
||||||
|
class DemoApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner
|
||||||
|
|
||||||
|
@RunWith(SpringRunner)
|
||||||
|
@SpringBootTest
|
||||||
|
class DemoApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringApplicationConfiguration(classes = DemoApplication.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
public class DemoApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class DemoApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner::class)
|
||||||
|
@SpringApplicationConfiguration(classes = arrayOf(DemoApplication::class))
|
||||||
|
@WebAppConfiguration
|
||||||
|
class DemoApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner
|
||||||
|
|
||||||
|
@RunWith(SpringRunner::class)
|
||||||
|
@SpringBootTest
|
||||||
|
class DemoApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user