Fixed spring-session integration tests (#4650)

This commit is contained in:
Amit Pandey 2018-07-08 16:57:45 +05:30 committed by Grzegorz Piwowarek
parent 3b7cb37379
commit 28166a73de
29 changed files with 258 additions and 101 deletions

View File

@ -16,7 +16,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class ArticleRepositoryIntegrationTest { public class ArticleRepositoryLiveTest {
private static final String TABLE_NAME = "articles"; private static final String TABLE_NAME = "articles";

View File

@ -5,8 +5,8 @@
<artifactId>spring-boot-dynamodb</artifactId> <artifactId>spring-boot-dynamodb</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Spring Boot Actuator</name> <name>spring-boot-dynamodb</name>
<description>This is simple boot application for Spring boot actuator test</description> <description>This is simple boot application for Spring boot dynamodb test</description>
<parent> <parent>
<artifactId>parent-boot-1</artifactId> <artifactId>parent-boot-1</artifactId>

View File

@ -1,14 +1,18 @@
package com.baeldung.spring.data.dynamodb.config; package com.baeldung.spring.data.dynamodb.config;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
@Configuration @Configuration
@EnableDynamoDBRepositories(basePackages = "com.baeldung.spring.data.dynamodb.repositories") @EnableDynamoDBRepositories(basePackages = "com.baeldung.spring.data.dynamodb.repositories")
@ -22,6 +26,9 @@ public class DynamoDBConfig {
@Value("${amazon.aws.secretkey}") @Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey; private String amazonAWSSecretKey;
@Autowired
private ApplicationContext context;
@Bean @Bean
public AmazonDynamoDB amazonDynamoDB() { public AmazonDynamoDB amazonDynamoDB() {
@ -37,4 +44,8 @@ public class DynamoDBConfig {
return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey); return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
} }
@Bean(name = "mvcHandlerMappingIntrospector")
public HandlerMappingIntrospector mvcHandlerMappingIntrospector() {
return new HandlerMappingIntrospector(context);
}
} }

View File

@ -70,6 +70,11 @@
<artifactId>spring-data-commons</artifactId> <artifactId>spring-data-commons</artifactId>
<version>${spring-data-commons.version}</version> <version>${spring-data-commons.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
@ -79,6 +84,7 @@
<jedis.version>2.9.0</jedis.version> <jedis.version>2.9.0</jedis.version>
<nosqlunit.version>0.10.0</nosqlunit.version> <nosqlunit.version>0.10.0</nosqlunit.version>
<spring-data-commons.version>2.0.3.RELEASE</spring-data-commons.version> <spring-data-commons.version>2.0.3.RELEASE</spring-data-commons.version>
<embedded-redis.version>0.6</embedded-redis.version>
</properties> </properties>
</project> </project>

View File

@ -1,8 +1,10 @@
package com.baeldung.spring.data.redis.config; package com.baeldung.spring.data.redis.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.ChannelTopic;
@ -18,6 +20,7 @@ import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
@Configuration @Configuration
@ComponentScan("com.baeldung.spring.data.redis") @ComponentScan("com.baeldung.spring.data.redis")
@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo") @EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo")
@PropertySource("classpath:application.properties")
public class RedisConfig { public class RedisConfig {
@Bean @Bean

View File

@ -0,0 +1,2 @@
spring.redis.host=localhost
spring.redis.port=6379

View File

@ -1,24 +1,44 @@
package com.baeldung.spring.data.redis; package com.baeldung.spring.data.redis;
import com.baeldung.spring.data.redis.config.RedisConfig;
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.UUID;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.UUID;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.data.redis.config.RedisConfig;
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RedisConfig.class) @ContextConfiguration(classes = RedisConfig.class)
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class RedisMessageListenerIntegrationTest { public class RedisMessageListenerIntegrationTest {
private static redis.embedded.RedisServer redisServer;
@Autowired @Autowired
private RedisMessagePublisher redisMessagePublisher; private RedisMessagePublisher redisMessagePublisher;
@BeforeClass
public static void startRedisServer() throws IOException {
redisServer = new redis.embedded.RedisServer(6379);
redisServer.start();
}
@AfterClass
public static void stopRedisServer() throws IOException {
redisServer.stop();
}
@Test @Test
public void testOnMessage() throws Exception { public void testOnMessage() throws Exception {

View File

@ -3,13 +3,17 @@ package com.baeldung.spring.data.redis.repo;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -18,10 +22,24 @@ import com.baeldung.spring.data.redis.model.Student;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RedisConfig.class) @ContextConfiguration(classes = RedisConfig.class)
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class StudentRepositoryIntegrationTest { public class StudentRepositoryIntegrationTest {
@Autowired @Autowired
private StudentRepository studentRepository; private StudentRepository studentRepository;
private static redis.embedded.RedisServer redisServer;
@BeforeClass
public static void startRedisServer() throws IOException {
redisServer = new redis.embedded.RedisServer(6379);
redisServer.start();
}
@AfterClass
public static void stopRedisServer() throws IOException {
redisServer.stop();
}
@Test @Test
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {

View File

@ -1,12 +1,10 @@
package org.baeldung.spring.cloud; package org.baeldung.spring.cloud;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask; import org.springframework.cloud.task.configuration.EnableTask;
@EnableTask @EnableTask
@EnableBatchProcessing
@SpringBootApplication @SpringBootApplication
public class BatchJobApplication { public class BatchJobApplication {

View File

@ -2,9 +2,9 @@ package org.baeldung.spring.cloud;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Job;
import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.scope.context.ChunkContext;
@ -15,6 +15,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableBatchProcessing
public class JobConfiguration { public class JobConfiguration {
private static final Log logger = LogFactory.getLog(JobConfiguration.class); private static final Log logger = LogFactory.getLog(JobConfiguration.class);

View File

@ -3,10 +3,12 @@ package org.baeldung.spring.cloud;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@ContextConfiguration(classes = JobConfiguration.class)
public class BatchJobApplicationIntegrationTest { public class BatchJobApplicationIntegrationTest {
@Test @Test

View File

@ -18,6 +18,7 @@
<properties> <properties>
<rest-assured.version>3.0.1</rest-assured.version> <rest-assured.version>3.0.1</rest-assured.version>
<embedded-redis.version>0.6</embedded-redis.version>
</properties> </properties>
<dependencies> <dependencies>
@ -57,6 +58,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>

View File

@ -1,13 +1,33 @@
package org.baeldung; package org.baeldung;
import java.io.IOException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import redis.embedded.RedisServer;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class BookReviewsApiIntegrationTest { public class BookReviewsApiIntegrationTest {
private static RedisServer redisServer;
private static int port;
@BeforeClass
public static void setUp() throws IOException {
redisServer = new RedisServer(6379);
redisServer.start();
}
@AfterClass
public static void destroy() {
redisServer.stop();
}
@Test @Test
public void contextLoads() { public void contextLoads() {

View File

@ -5,7 +5,6 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -30,7 +29,7 @@ public class FileProcessorIntegrationTest {
assertTrue(file.getText().endsWith("processed")); assertTrue(file.getText().endsWith("processed"));
} }
@Test(expected=NoSuchBeanDefinitionException.class) @Test(expected=BeanCreationException.class)
public void whenDependentBeanNotAvailable_ThrowsNoSuchBeanDefinitionException(){ public void whenDependentBeanNotAvailable_ThrowsNoSuchBeanDefinitionException(){
context.getBean("dummyFileProcessor"); context.getBean("dummyFileProcessor");
} }

View File

@ -1,10 +1,11 @@
package com.baeldung; package com.baeldung;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions; import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber; import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class) @RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources") @CucumberOptions(features = "src/test/resources")
public class CucumberIntegrationTest { public class CucumberIntegrationTest extends SpringIntegrationTest{
} }

View File

@ -5,16 +5,17 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.ResponseErrorHandler; import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
//@RunWith(SpringJUnit4ClassRunner.class) //@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringDemoApplication.class) @SpringBootTest(classes = SpringDemoApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@WebAppConfiguration @ContextConfiguration
public class SpringIntegrationTest { public class SpringIntegrationTest {
static ResponseResults latestResponse = null; static ResponseResults latestResponse = null;

View File

@ -25,7 +25,7 @@ import com.baeldung.repositories.BookRepository;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT) @SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class SpringDataProjectionIntegrationTest { public class SpringDataProjectionLiveTest {
private static final String BOOK_ENDPOINT = "http://localhost:8080/books"; private static final String BOOK_ENDPOINT = "http://localhost:8080/books";
private static final String AUTHOR_ENDPOINT = "http://localhost:8080/authors"; private static final String AUTHOR_ENDPOINT = "http://localhost:8080/authors";

View File

@ -1,15 +1,14 @@
package com.baeldung.relationships; package com.baeldung.relationships;
import com.baeldung.SpringDataRestApplication; import static org.junit.Assert.assertEquals;
import com.baeldung.models.Address;
import com.baeldung.models.Author;
import com.baeldung.models.Book;
import com.baeldung.models.Library;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
@ -18,21 +17,27 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.json.JSONException;
import static org.junit.Assert.assertEquals; import com.baeldung.SpringDataRestApplication;
import com.baeldung.models.Address;
import com.baeldung.models.Author;
import com.baeldung.models.Book;
import com.baeldung.models.Library;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT) @SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class SpringDataRelationshipsIntegrationTest { public class SpringDataRelationshipsIntegrationTest {
@Autowired @Autowired
private TestRestTemplate template; private TestRestTemplate template;
@Value("${local.server.port}")
private int port;
private static final String BOOK_ENDPOINT = "http://localhost:8080/books/"; private static final String BOOK_ENDPOINT = "http://localhost:%s/books/";
private static final String AUTHOR_ENDPOINT = "http://localhost:8080/authors/"; private static final String AUTHOR_ENDPOINT = "http://localhost:%s/authors/";
private static final String ADDRESS_ENDPOINT = "http://localhost:8080/addresses/"; private static final String ADDRESS_ENDPOINT = "http://localhost:%s/addresses/";
private static final String LIBRARY_ENDPOINT = "http://localhost:8080/libraries/"; private static final String LIBRARY_ENDPOINT = "http://localhost:%s/libraries/";
private static final String LIBRARY_NAME = "My Library"; private static final String LIBRARY_NAME = "My Library";
private static final String AUTHOR_NAME = "George Orwell"; private static final String AUTHOR_NAME = "George Orwell";
@ -40,17 +45,17 @@ public class SpringDataRelationshipsIntegrationTest {
@Test @Test
public void whenSaveOneToOneRelationship_thenCorrect() throws JSONException { public void whenSaveOneToOneRelationship_thenCorrect() throws JSONException {
Library library = new Library(LIBRARY_NAME); Library library = new Library(LIBRARY_NAME);
template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); template.postForEntity(String.format(LIBRARY_ENDPOINT, port), library, Library.class);
Address address = new Address("Main street, nr 1"); Address address = new Address("Main street, nr 1");
template.postForEntity(ADDRESS_ENDPOINT, address, Address.class); template.postForEntity(String.format(ADDRESS_ENDPOINT, port), address, Address.class);
HttpHeaders requestHeaders = new HttpHeaders(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-type", "text/uri-list"); requestHeaders.add("Content-type", "text/uri-list");
HttpEntity<String> httpEntity = new HttpEntity<>(ADDRESS_ENDPOINT + "/1", requestHeaders); HttpEntity<String> httpEntity = new HttpEntity<>(String.format(ADDRESS_ENDPOINT, port) + "/1", requestHeaders);
template.exchange(LIBRARY_ENDPOINT + "/1/libraryAddress", HttpMethod.PUT, httpEntity, String.class); template.exchange(String.format(LIBRARY_ENDPOINT, port) + "/1/libraryAddress", HttpMethod.PUT, httpEntity, String.class);
ResponseEntity<Library> libraryGetResponse = template.getForEntity(ADDRESS_ENDPOINT + "/1/library", Library.class); ResponseEntity<Library> libraryGetResponse = template.getForEntity(String.format(ADDRESS_ENDPOINT, port) + "/1/library", Library.class);
assertEquals("library is incorrect", libraryGetResponse.getBody() assertEquals("library is incorrect", libraryGetResponse.getBody()
.getName(), LIBRARY_NAME); .getName(), LIBRARY_NAME);
} }
@ -58,21 +63,21 @@ public class SpringDataRelationshipsIntegrationTest {
@Test @Test
public void whenSaveOneToManyRelationship_thenCorrect() throws JSONException{ public void whenSaveOneToManyRelationship_thenCorrect() throws JSONException{
Library library = new Library(LIBRARY_NAME); Library library = new Library(LIBRARY_NAME);
template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); template.postForEntity(String.format(LIBRARY_ENDPOINT, port), library, Library.class);
Book book1 = new Book("Dune"); Book book1 = new Book("Dune");
template.postForEntity(BOOK_ENDPOINT, book1, Book.class); template.postForEntity(String.format(BOOK_ENDPOINT, port), book1, Book.class);
Book book2 = new Book("1984"); Book book2 = new Book("1984");
template.postForEntity(BOOK_ENDPOINT, book2, Book.class); template.postForEntity(String.format(BOOK_ENDPOINT, port), book2, Book.class);
HttpHeaders requestHeaders = new HttpHeaders(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-type", "text/uri-list"); requestHeaders.add("Content-type", "text/uri-list");
HttpEntity<String> bookHttpEntity = new HttpEntity<>(LIBRARY_ENDPOINT + "/1", requestHeaders); HttpEntity<String> bookHttpEntity = new HttpEntity<>(String.format(LIBRARY_ENDPOINT, port) + "/1", requestHeaders);
template.exchange(BOOK_ENDPOINT + "/1/library", HttpMethod.PUT, bookHttpEntity, String.class); template.exchange(String.format(BOOK_ENDPOINT, port) + "/1/library", HttpMethod.PUT, bookHttpEntity, String.class);
template.exchange(BOOK_ENDPOINT + "/2/library", HttpMethod.PUT, bookHttpEntity, String.class); template.exchange(String.format(BOOK_ENDPOINT, port) + "/2/library", HttpMethod.PUT, bookHttpEntity, String.class);
ResponseEntity<Library> libraryGetResponse = template.getForEntity(BOOK_ENDPOINT + "/1/library", Library.class); ResponseEntity<Library> libraryGetResponse = template.getForEntity(String.format(BOOK_ENDPOINT, port) + "/1/library", Library.class);
assertEquals("library is incorrect", libraryGetResponse.getBody() assertEquals("library is incorrect", libraryGetResponse.getBody()
.getName(), LIBRARY_NAME); .getName(), LIBRARY_NAME);
} }
@ -80,20 +85,20 @@ public class SpringDataRelationshipsIntegrationTest {
@Test @Test
public void whenSaveManyToManyRelationship_thenCorrect() throws JSONException{ public void whenSaveManyToManyRelationship_thenCorrect() throws JSONException{
Author author1 = new Author(AUTHOR_NAME); Author author1 = new Author(AUTHOR_NAME);
template.postForEntity(AUTHOR_ENDPOINT, author1, Author.class); template.postForEntity(String.format(AUTHOR_ENDPOINT, port), author1, Author.class);
Book book1 = new Book("Animal Farm"); Book book1 = new Book("Animal Farm");
template.postForEntity(BOOK_ENDPOINT, book1, Book.class); template.postForEntity(String.format(BOOK_ENDPOINT, port), book1, Book.class);
Book book2 = new Book("1984"); Book book2 = new Book("1984");
template.postForEntity(BOOK_ENDPOINT, book2, Book.class); template.postForEntity(String.format(BOOK_ENDPOINT, port), book2, Book.class);
HttpHeaders requestHeaders = new HttpHeaders(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-type", "text/uri-list"); requestHeaders.add("Content-type", "text/uri-list");
HttpEntity<String> httpEntity = new HttpEntity<>(BOOK_ENDPOINT + "/1\n" + BOOK_ENDPOINT + "/2", requestHeaders); HttpEntity<String> httpEntity = new HttpEntity<>(String.format(BOOK_ENDPOINT, port) + "/1\n" + String.format(BOOK_ENDPOINT, port) + "/2", requestHeaders);
template.exchange(AUTHOR_ENDPOINT + "/1/books", HttpMethod.PUT, httpEntity, String.class); template.exchange(String.format(AUTHOR_ENDPOINT, port) + "/1/books", HttpMethod.PUT, httpEntity, String.class);
String jsonResponse = template.getForObject(BOOK_ENDPOINT + "/1/authors", String.class); String jsonResponse = template.getForObject(String.format(BOOK_ENDPOINT, port) + "/1/authors", String.class);
JSONObject jsonObj = new JSONObject(jsonResponse).getJSONObject("_embedded"); JSONObject jsonObj = new JSONObject(jsonResponse).getJSONObject("_embedded");
JSONArray jsonArray = jsonObj.getJSONArray("authors"); JSONArray jsonArray = jsonObj.getJSONArray("authors");
assertEquals("author is incorrect", jsonArray.getJSONObject(0) assertEquals("author is incorrect", jsonArray.getJSONObject(0)

View File

@ -1,31 +1,33 @@
package com.baeldung.validator; package com.baeldung.validator;
import com.baeldung.SpringDataRestApplication;
import com.baeldung.models.WebsiteUser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.SpringDataRestApplication;
import com.baeldung.models.WebsiteUser;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringDataRestApplication.class) @SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@WebAppConfiguration @AutoConfigureMockMvc
public class SpringDataRestValidatorIntegrationTest { public class SpringDataRestValidatorIntegrationTest {
public static final String URL = "http://localhost"; public static final String URL = "http://localhost";
@Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@Autowired @Autowired

View File

@ -29,6 +29,12 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>${de.flapdoodle.embed.mongo.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -73,6 +79,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<checkstyle.version>2.17</checkstyle.version> <checkstyle.version>2.17</checkstyle.version>
<de.flapdoodle.embed.mongo.version>2.0.0</de.flapdoodle.embed.mongo.version>
</properties> </properties>
</project> </project>

View File

@ -13,7 +13,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest @SpringBootTest(classes = {SpringJenkinsPipelineApplication.class, TestMongoConfig.class })
public class SomeIntegrationTest { public class SomeIntegrationTest {
@Autowired @Autowired
private StudentRepository studentRepository; private StudentRepository studentRepository;

View File

@ -0,0 +1,11 @@
package com.baeldung;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class })
public class TestMongoConfig {
}

View File

@ -122,6 +122,14 @@
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>${json.version}</version> <version>${json.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-logger-api</artifactId>
<version>${maven-surefire-plugin.version}</version>
<!-- to get around bug https://github.com/junit-team/junit5/issues/1367 -->
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
<build> <build>
<pluginManagement> <pluginManagement>

View File

@ -21,7 +21,7 @@ import com.baeldung.transfer.LoginForm;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RestClientConfig.class) @ContextConfiguration(classes = RestClientConfig.class)
public class RestTemplateIntegrationTest { public class RestTemplateLiveTest {
@Autowired @Autowired
RestTemplate restTemplate; RestTemplate restTemplate;

View File

@ -30,6 +30,15 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
</dependencies> </dependencies>
<properties>
<embedded-redis.version>0.6</embedded-redis.version>
</properties>
</project> </project>

View File

@ -1,27 +1,55 @@
package com.baeldung.spring.session; package com.baeldung.spring.session;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import redis.clients.jedis.Jedis;
import java.util.Set;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Set;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import redis.embedded.RedisServer;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SessionWebApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class SessionControllerIntegrationTest { public class SessionControllerIntegrationTest {
private Jedis jedis; private Jedis jedis;
private static RedisServer redisServer;
private TestRestTemplate testRestTemplate; private TestRestTemplate testRestTemplate;
private TestRestTemplate testRestTemplateWithAuth; private TestRestTemplate testRestTemplateWithAuth;
private String testUrl = "http://localhost:8080/"; private String testUrl = "http://localhost:8080/";
@BeforeClass
public static void startRedisServer() throws IOException {
redisServer = new RedisServer(6379);
redisServer.start();
}
@AfterClass
public static void stopRedisServer() throws IOException {
redisServer.stop();
}
@Before @Before
public void clearRedisData() { public void clearRedisData() {
testRestTemplate = new TestRestTemplate(); testRestTemplate = new TestRestTemplate();
testRestTemplateWithAuth = new TestRestTemplate("admin", "password", null); testRestTemplateWithAuth = new TestRestTemplate("admin", "password");
jedis = new Jedis("localhost", 6379); jedis = new Jedis("localhost", 6379);
jedis.flushAll(); jedis.flushAll();

View File

@ -217,11 +217,11 @@
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.security.version>4.2.6.RELEASE</org.springframework.security.version> <org.springframework.security.version>4.2.6.RELEASE</org.springframework.security.version>
<spring-boot.version>1.4.2.RELEASE</spring-boot.version> <spring-boot.version>1.5.14.RELEASE</spring-boot.version>
<javassist.version>3.21.0-GA</javassist.version> <javassist.version>3.21.0-GA</javassist.version>
<!-- persistence --> <!-- persistence -->
<hibernate.version>5.2.5.Final</hibernate.version> <hibernate.version>5.2.10.Final</hibernate.version>
<mysql-connector-java.version>5.1.40</mysql-connector-java.version> <mysql-connector-java.version>5.1.40</mysql-connector-java.version>
<spring-data-jpa.version>1.10.5.RELEASE</spring-data-jpa.version> <spring-data-jpa.version>1.10.5.RELEASE</spring-data-jpa.version>
<h2.version>1.4.193</h2.version> <h2.version>1.4.193</h2.version>

View File

@ -7,6 +7,6 @@ jdbc.pass=tutorialpass
# hibernate.X # hibernate.X
hibernate.dialect=org.hibernate.dialect.DerbyDialect hibernate.dialect=org.hibernate.dialect.DerbyDialect
hibernate.show_sql=false hibernate.show_sql=false
hibernate.hbm2ddl.auto=create hibernate.hbm2ddl.auto=update
hibernate.cache.use_second_level_cache=false hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false hibernate.cache.use_query_cache=false

View File

@ -1,5 +1,10 @@
package org.baeldung.userservice; package org.baeldung.userservice;
import static org.junit.Assert.assertEquals;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.baeldung.custom.config.MvcConfig; import org.baeldung.custom.config.MvcConfig;
import org.baeldung.custom.config.PersistenceDerbyJPAConfig; import org.baeldung.custom.config.PersistenceDerbyJPAConfig;
import org.baeldung.custom.config.SecSecurityConfig; import org.baeldung.custom.config.SecSecurityConfig;
@ -8,22 +13,16 @@ import org.baeldung.web.MyUserDto;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.*;
import java.util.logging.Level; @RunWith(SpringRunner.class)
import java.util.logging.Logger; @SpringBootTest(classes = { MvcConfig.class, PersistenceDerbyJPAConfig.class, SecSecurityConfig.class })
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { MvcConfig.class, PersistenceDerbyJPAConfig.class, SecSecurityConfig.class })
@WebAppConfiguration @WebAppConfiguration
public class CustomUserDetailsServiceIntegrationTest { public class CustomUserDetailsServiceIntegrationTest {