diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 55851b7721..b3e249a025 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -55,9 +55,10 @@ spring-boot-sample-secure spring-boot-sample-servlet spring-boot-sample-simple + spring-boot-sample-testng + spring-boot-sample-tomcat spring-boot-sample-tomcat-jsp spring-boot-sample-tomcat-ssl - spring-boot-sample-tomcat spring-boot-sample-tomcat-multi-connectors spring-boot-sample-tomcat7-jsp spring-boot-sample-traditional diff --git a/spring-boot-samples/spring-boot-sample-testng/.gitignore b/spring-boot-samples/spring-boot-sample-testng/.gitignore new file mode 100644 index 0000000000..e93dfd32bf --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-testng/.gitignore @@ -0,0 +1 @@ +test-output diff --git a/spring-boot-samples/spring-boot-sample-testng/pom.xml b/spring-boot-samples/spring-boot-sample-testng/pom.xml new file mode 100644 index 0000000000..f4e4488b4a --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-testng/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-samples + 1.2.1.BUILD-SNAPSHOT + + spring-boot-sample-testng + Spring Boot TestNG Sample + Spring Boot TestNG Sample + http://projects.spring.io/spring-boot/ + + Pivotal Software, Inc. + http://www.spring.io + + + ${basedir}/../.. + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-tomcat + + + org.springframework + spring-webmvc + + + org.springframework + spring-test + + + org.testng + testng + 6.8.13 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/SampleTestNGApplication.java b/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/SampleTestNGApplication.java new file mode 100644 index 0000000000..e84bd95a08 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/SampleTestNGApplication.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.testng; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class SampleTestNGApplication { + + private static Log logger = LogFactory.getLog(SampleTestNGApplication.class); + + @Bean + protected ServletContextListener listener() { + return new ServletContextListener() { + + @Override + public void contextInitialized(ServletContextEvent sce) { + logger.info("ServletContext initialized"); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + logger.info("ServletContext destroyed"); + } + + }; + } + + public static void main(String[] args) throws Exception { + SpringApplication.run(SampleTestNGApplication.class, args); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/service/HelloWorldService.java b/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/service/HelloWorldService.java new file mode 100644 index 0000000000..e84f67c9ec --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/service/HelloWorldService.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.testng.service; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class HelloWorldService { + + @Value("${name:World}") + private String name; + + public String getHelloMessage() { + return "Hello " + this.name; + } + +} diff --git a/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/web/SampleController.java b/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/web/SampleController.java new file mode 100644 index 0000000000..07812d3c83 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-testng/src/main/java/sample/testng/web/SampleController.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.testng.web; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import sample.testng.service.HelloWorldService; + +@Controller +public class SampleController { + + @Autowired + private HelloWorldService helloWorldService; + + @RequestMapping("/") + @ResponseBody + public String helloWorld() { + return this.helloWorldService.getHelloMessage(); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-testng/src/main/resources/public/test.css b/spring-boot-samples/spring-boot-sample-testng/src/main/resources/public/test.css new file mode 100644 index 0000000000..a6e538f4a4 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-testng/src/main/resources/public/test.css @@ -0,0 +1 @@ +p.{} diff --git a/spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java b/spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java new file mode 100644 index 0000000000..38ebfe872e --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java @@ -0,0 +1,53 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.testng; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; +import org.testng.annotations.Test; + +import sample.testng.SampleTestNGApplication; +import static org.testng.Assert.assertEquals; + +/** + * Basic integration tests for demo application. + * + * @author Phillip Webb + */ +@SpringApplicationConfiguration(classes = SampleTestNGApplication.class) +@WebIntegrationTest("server.port:0") +@DirtiesContext +public class SampleTestNGApplicationTests extends AbstractTestNGSpringContextTests { + + @Value("${local.server.port}") + private int port; + + @Test + public void testHome() throws Exception { + ResponseEntity entity = new TestRestTemplate().getForEntity( + "http://localhost:" + this.port, String.class); + assertEquals(HttpStatus.OK, entity.getStatusCode()); + assertEquals("Hello World", entity.getBody()); + } + +}