mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 17:48:14 +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_4_0_M2 = Version.parse('1.4.0.M2')
|
||||
|
||||
@Autowired
|
||||
ApplicationEventPublisher eventPublisher
|
||||
|
||||
@@ -138,13 +140,7 @@ class ProjectGenerator {
|
||||
|
||||
def test = new File(new File(dir, "src/test/$codeLocation"), request.packageName.replace('.', '/'))
|
||||
test.mkdirs()
|
||||
if (request.hasWebFacet()) {
|
||||
model.testAnnotations = '@WebAppConfiguration\n'
|
||||
model.testImports = 'import org.springframework.test.context.web.WebAppConfiguration;\n'
|
||||
} else {
|
||||
model.testAnnotations = ''
|
||||
model.testImports = ''
|
||||
}
|
||||
setupTestModel(request, model)
|
||||
write(new File(test, "${applicationName}Tests.${extension}"), "ApplicationTests.$extension", model)
|
||||
|
||||
def resources = new File(dir, 'src/main/resources')
|
||||
@@ -228,9 +224,46 @@ class ProjectGenerator {
|
||||
model['bootOneThreeAvailable'] = VERSION_1_3_0_M1
|
||||
.compareTo(Version.safeParse(request.bootVersion)) <= 0
|
||||
|
||||
// New testing stuff
|
||||
model['newTestInfrastructure'] = isNewTestInfrastructureAvailable(request)
|
||||
|
||||
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) {
|
||||
template 'starter-pom.xml', model
|
||||
}
|
||||
|
@@ -2,11 +2,11 @@ package ${packageName}
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
${testImports}import org.springframework.boot.test.SpringApplicationConfiguration
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
||||
|
||||
${testImports}<% if (newTestInfrastructure) { %>
|
||||
@RunWith(SpringRunner)
|
||||
@SpringBootTest<% } else { %>
|
||||
@RunWith(SpringJUnit4ClassRunner)
|
||||
@SpringApplicationConfiguration(classes = ${applicationName})
|
||||
@SpringApplicationConfiguration(classes = ${applicationName})<% } %>
|
||||
${testAnnotations}class ${applicationName}Tests {
|
||||
|
||||
@Test
|
||||
|
@@ -2,11 +2,11 @@ package ${packageName};
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
${testImports}import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
${testImports}<% if (newTestInfrastructure) { %>
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest<% } else { %>
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ${applicationName}.class)
|
||||
@SpringApplicationConfiguration(classes = ${applicationName}.class)<% } %>
|
||||
${testAnnotations}public class ${applicationName}Tests {
|
||||
|
||||
@Test
|
||||
|
@@ -2,11 +2,11 @@ package ${packageName}
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
${testImports}import org.springframework.boot.test.SpringApplicationConfiguration
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
||||
|
||||
${testImports}<% if (newTestInfrastructure) { %>
|
||||
@RunWith(SpringRunner::class)
|
||||
@SpringBootTest<% } else { %>
|
||||
@RunWith(SpringJUnit4ClassRunner::class)
|
||||
@SpringApplicationConfiguration(classes = arrayOf(${applicationName}::class))
|
||||
@SpringApplicationConfiguration(classes = arrayOf(${applicationName}::class))<% } %>
|
||||
${testAnnotations}class ${applicationName}Tests {
|
||||
|
||||
@Test
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package io.spring.initializr.generator
|
||||
|
||||
import io.spring.initializr.metadata.Dependency
|
||||
import io.spring.initializr.metadata.InitializrMetadata
|
||||
import io.spring.initializr.metadata.SimpleInitializrMetadataProvider
|
||||
import io.spring.initializr.test.generator.GradleBuildAssert
|
||||
@@ -49,8 +50,11 @@ abstract class AbstractProjectGeneratorTests {
|
||||
|
||||
@Before
|
||||
void setup() {
|
||||
def web = new Dependency(id: 'web')
|
||||
web.facets << 'web'
|
||||
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)
|
||||
projectGenerator.eventPublisher = eventPublisher
|
||||
projectGenerator.tmpdir = folder.newFolder().absolutePath
|
||||
|
@@ -89,6 +89,16 @@ class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests {
|
||||
.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
|
||||
public void standardServletInitializer() {
|
||||
def request = createProjectRequest()
|
||||
@@ -99,4 +109,26 @@ class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests {
|
||||
.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