Merge remote-tracking branch 'origin/master'

This commit is contained in:
slavisa-baeldung 2017-04-05 06:04:20 +01:00
commit 8ee89acfb5
31 changed files with 605 additions and 39 deletions

View File

@ -0,0 +1,21 @@
package com.baeldung.stringtokenizer;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Application {
public List<String> getTokens(String str) {
List<String> tokens = new ArrayList<String>();
// StringTokenizer tokenizer = new StringTokenizer( str );
StringTokenizer tokenizer = new StringTokenizer( str , "," );
// StringTokenizer tokenizer = new StringTokenizer( str , "," , true );
while (tokenizer.hasMoreElements()) {
tokens.add( tokenizer.nextToken() );
// tokens.add( tokenizer.nextToken( "," ) );
}
return tokens;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.junit4vstestng;
import static org.junit.Assert.assertTrue;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SortedTests {
@Test
public void a_givenString_whenChangedtoInt_thenTrue() {
assertTrue(Integer.valueOf("10") instanceof Integer);
}
@Test
public void b_givenInt_whenChangedtoString_thenTrue() {
assertTrue(String.valueOf(10) instanceof String);
}
}

View File

@ -17,7 +17,7 @@ public class FlattenNestedListTest {
List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four"));
@Test
public void givenString_flattenNestedList1() {
public void givenNestedList_thenFlattenNestedListImperative() {
List<String> ls = flattenListOfListsImperatively(lol);
assertNotNull(ls);
@ -27,7 +27,7 @@ public class FlattenNestedListTest {
}
@Test
public void givenString_flattenNestedList2() {
public void givenNestedList_thenFlattenNestedListStream() {
List<String> ls = flattenListOfListsStream(lol);
assertNotNull(ls);

View File

@ -0,0 +1,30 @@
package com.baeldung.stringtokenizer;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ApplicationTest {
Application application = new Application();
List<String> expectedTokens = new ArrayList<String>();
@Before
public void init() {
expectedTokens.add( "Welcome" );
expectedTokens.add( "to" );
expectedTokens.add( "baeldung.com" );
}
@Test
public void givenString_thenGetListOfString() {
String str = "Welcome,to,baeldung.com";
List<String> actualTokens = application.getTokens(str);
assertEquals(expectedTokens, actualTokens);
}
}

View File

@ -14,9 +14,9 @@ import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class JsoupParserTest {
public class JsoupParserIntegrationTest {
Document doc;
private Document doc;
@Before
public void setUp() throws IOException {

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
@ -72,6 +72,26 @@
<artifactId>javers-core</artifactId>
<version>${javers.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
</dependencies>
<properties>
@ -84,6 +104,9 @@
<assertj.version>3.6.2</assertj.version>
<jsonassert.version>1.5.0</jsonassert.version>
<javers.version>3.1.0</javers.version>
<jetty.version>9.4.2.v20170220</jetty.version>
<httpclient.version>4.5.3</httpclient.version>
<commons.io.version>2.5</commons.io.version>
</properties>
</project>

View File

@ -0,0 +1,42 @@
package com.baeldung.jetty;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class AsyncServlet extends HttpServlet {
private static final String HEAVY_RESOURCE = "This is some heavy resource that will be served in an async way";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ByteBuffer content = ByteBuffer.wrap(HEAVY_RESOURCE.getBytes(StandardCharsets.UTF_8));
AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (out.isReady()) {
if (!content.hasRemaining()) {
response.setStatus(200);
async.complete();
return;
}
out.write(content.get());
}
}
@Override
public void onError(Throwable t) {
getServletContext().log("Async Error", t);
async.complete();
}
});
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.jetty;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class BlockingServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("{ \"status\": \"ok\"}");
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.jetty;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletHandler;
public class JettyServer {
private Server server;
public void start() throws Exception {
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8090);
server.setConnectors(new Connector[]{connector});
ServletHandler servletHandler = new ServletHandler();
server.setHandler(servletHandler);
servletHandler.addServletWithMapping(BlockingServlet.class, "/status");
servletHandler.addServletWithMapping(AsyncServlet.class, "/heavy/async");
server.start();
}
public void stop() throws Exception {
server.stop();
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.jetty;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
public class JettyTest {
private JettyServer jettyServer;
@Before
public void setup() throws Exception {
jettyServer = new JettyServer();
jettyServer.start();
}
@After
public void cleanup() throws Exception {
Thread.sleep(2000);
jettyServer.stop();
}
@Test
public void givenServer_whenSendRequestToBlockingServlet_thenReturnStatusOK() throws Exception {
//given
String url = "http://localhost:8090/status";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
//then
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
}
@Test
public void givenServer_whenSendRequestToNonBlockingServlet_thenReturnStatusOK() throws Exception {
//when
String url = "http://localhost:8090/heavy/async";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
//then
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
assertThat(responseContent).isEqualTo("This is some heavy resource that will be served in an async way");
}
}

View File

@ -129,6 +129,7 @@
<module>spring-data-couchbase-2</module>
<module>spring-data-dynamodb</module>
<module>spring-data-elasticsearch</module>
<module>spring-data-javaslang</module>
<module>spring-data-mongodb</module>
<module>spring-data-neo4j</module>
<module>spring-data-redis</module>

View File

@ -46,3 +46,4 @@ public class Application {
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.filter;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
public class CORSFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
}
}

View File

@ -4,7 +4,8 @@ import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;
public class MyBeanNotOfRequiredTypeFailureAnalyzer extends AbstractFailureAnalyzer<BeanNotOfRequiredTypeException> {
public class MyBeanNotOfRequiredTypeFailureAnalyzer
extends AbstractFailureAnalyzer<BeanNotOfRequiredTypeException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, BeanNotOfRequiredTypeException cause) {
@ -12,14 +13,16 @@ public class MyBeanNotOfRequiredTypeFailureAnalyzer extends AbstractFailureAnaly
}
private String getDescription(BeanNotOfRequiredTypeException ex) {
return "The bean " + ex.getBeanName() //
+ " could not be injected as " + ex.getRequiredType().getName() //
+ " because it is of type " + ex.getActualType().getName();
return String.format("The bean %s could not be injected as %s because it is of type %s",
ex.getBeanName(),
ex.getRequiredType().getName(),
ex.getActualType().getName());
}
private String getAction(BeanNotOfRequiredTypeException ex) {
return "Consider creating a bean with name "+ ex.getBeanName() //
+ " of type " + ex.getRequiredType().getName();
return String.format("Consider creating a bean with name %s of type %s",
ex.getBeanName(),
ex.getRequiredType().getName());
}
}

View File

@ -0,0 +1,41 @@
package org.baeldung.boot.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}

View File

@ -0,0 +1,10 @@
package org.baeldung.boot.repository;
import org.baeldung.boot.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository("userRepository")
public interface UserRepository extends JpaRepository<User, Integer> {
public int countByStatus(int status);
}

View File

@ -0,0 +1,26 @@
package org.baeldung.endpoints.info;
import java.util.HashMap;
import java.util.Map;
import org.baeldung.boot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
@Component
public class TotalUsersInfoContributor implements InfoContributor {
@Autowired
UserRepository userRepository;
@Override
public void contribute(Info.Builder builder) {
Map<String, Integer> userDetails = new HashMap<>();
userDetails.put("active", userRepository.countByStatus(1));
userDetails.put("inactive", userRepository.countByStatus(0));
builder.withDetail("users", userDetails);
}
}

View File

@ -0,0 +1,15 @@
package org.baeldung.jsoncomponent;
import javafx.scene.paint.Color;
public class User {
private final Color favoriteColor;
public User(Color favoriteColor) {
this.favoriteColor = favoriteColor;
}
public Color getFavoriteColor() {
return favoriteColor;
}
}

View File

@ -0,0 +1,46 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.node.TextNode;
import javafx.scene.paint.Color;
import org.springframework.boot.jackson.JsonComponent;
import java.io.IOException;
@JsonComponent
public class UserCombinedSerializer {
public static class UserJsonSerializer extends JsonSerializer<User> {
@Override
public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("favoriteColor",
getColorAsWebColor(user.getFavoriteColor()));
jsonGenerator.writeEndObject();
}
private static String getColorAsWebColor(Color color) {
int r = (int) Math.round(color.getRed() * 255.0);
int g = (int) Math.round(color.getGreen() * 255.0);
int b = (int) Math.round(color.getBlue() * 255.0);
return String.format("#%02x%02x%02x", r, g, b);
}
}
@JsonComponent
public static class UserJsonDeserializer extends JsonDeserializer<User> {
@Override
public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
TextNode favoriteColor = (TextNode) treeNode.get("favoriteColor");
return new User(Color.web(favoriteColor.asText()));
}
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.TextNode;
import javafx.scene.paint.Color;
import org.springframework.boot.jackson.JsonComponent;
import java.io.IOException;
@JsonComponent
public class UserJsonDeserializer extends JsonDeserializer<User> {
@Override
public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
TextNode favoriteColor = (TextNode) treeNode.get("favoriteColor");
return new User(Color.web(favoriteColor.asText()));
}
}

View File

@ -0,0 +1,29 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import javafx.scene.paint.Color;
import org.springframework.boot.jackson.JsonComponent;
import java.io.IOException;
@JsonComponent
public class UserJsonSerializer extends JsonSerializer<User> {
@Override
public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("favoriteColor",
getColorAsWebColor(user.getFavoriteColor()));
jsonGenerator.writeEndObject();
}
private static String getColorAsWebColor(Color color) {
int r = (int) Math.round(color.getRed() * 255.0);
int g = (int) Math.round(color.getGreen() * 255.0);
int b = (int) Math.round(color.getBlue() * 255.0);
return String.format("#%02x%02x%02x", r, g, b);
}
}

View File

@ -2,7 +2,9 @@ server.port=8080
server.contextPath=/springbootapp
management.port=8081
management.address=127.0.0.1
#debug=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto = update
endpoints.shutdown.enabled=true
endpoints.jmx.domain=Spring Sample Application
@ -22,6 +24,7 @@ http.mappers.jsonPrettyPrint=true
info.app.name=Spring Sample Application
info.app.description=This is my first spring boot application G1
info.app.version=1.0.0
info.java-vendor = ${java.specification.vendor}
## Spring Security Configurations
security.user.name=admin1

View File

@ -0,0 +1,5 @@
insert into users values (1, 'Alex', 1);
insert into users values (2, 'Bob', 1);
insert into users values (3, 'John', 0);
insert into users values (4, 'Harry', 0);
insert into users values (5, 'Smith', 1);

View File

@ -0,0 +1,6 @@
create table USERS(
ID int not null AUTO_INCREMENT,
NAME varchar(100) not null,
STATUS int,
PRIMARY KEY ( ID )
);

View File

@ -0,0 +1,27 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.scene.paint.Color;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
@JsonTest
@RunWith(SpringRunner.class)
public class UserJsonDeserializerTest {
@Autowired
private ObjectMapper objectMapper;
@Test
public void testDeserialize() throws IOException {
User user = objectMapper.readValue("{\"favoriteColor\":\"#f0f8ff\"}", User.class);
assertEquals(Color.ALICEBLUE, user.getFavoriteColor());
}
}

View File

@ -0,0 +1,27 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.scene.paint.Color;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@JsonTest
@RunWith(SpringRunner.class)
public class UserJsonSerializerTest {
@Autowired
private ObjectMapper objectMapper;
@Test
public void testSerialization() throws JsonProcessingException {
String json = objectMapper.writeValueAsString(new User(Color.ALICEBLUE));
assertEquals("{\"favoriteColor\":\"#f0f8ff\"}", json);
}
}

View File

@ -75,6 +75,12 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>

View File

@ -18,13 +18,13 @@ import static junit.framework.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=GemfireConfiguration.class, loader=AnnotationConfigContextLoader.class)
public class EmployeeRepositoryTest {
public class EmployeeRepositoryIntegrationTest {
@Autowired
@Autowired private
EmployeeRepository employeeRepository;
@Autowired
FunctionExecution execution;
private FunctionExecution execution;
@Test
public void whenEmployeeIsSaved_ThenAbleToRead(){

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-data-javaslangb</groupId>
<artifactId>spring-data-javaslangb</artifactId>
<groupId>spring-data-javaslang</groupId>
<artifactId>spring-data-javaslang</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -65,7 +65,18 @@
<configuration>
<testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory>
</configuration>
</plugin>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>

View File

@ -1,34 +1,33 @@
package com.baeldung.spring_data_tests;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.baeldung.spring_data_app.MainApp;
import com.baeldung.spring_data.model.Book;
import com.baeldung.spring_data.model.JavaBook;
import com.baeldung.spring_data.repository.BookRepository;
import com.baeldung.spring_data.repository.JavaBookRepository;
import com.baeldung.spring_data_app.MainApp;
import javaslang.collection.List;
import javaslang.collection.Seq;
import javaslang.control.Option;
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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
import javaslang.collection.Seq;
import javaslang.collection.List;
import javaslang.control.Option;
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApp.class,webEnvironment = WebEnvironment.NONE)
public class SpringTests {
public class SpringIntegrationTest {
@Autowired
JavaBookRepository javaRepository;
private JavaBookRepository javaRepository;
@Autowired
BookRepository repository;
private BookRepository repository;
@Test
public void should_return_seq(){
@ -38,7 +37,8 @@ public class SpringTests {
testBook.setAuthors(authors);
Book book = repository.save(testBook);
Option<Seq<Book>> books = repository.findByTitleContaining("Seq Test");
assert(!books.isEmpty());
assertThat(books).isNotEmpty();
}
@ -50,8 +50,9 @@ public class SpringTests {
testBook.setAuthors(authors);
Book book = repository.save(testBook);
Option<Book> retBook = repository.findById(1L);
assert(retBook.isDefined() && !retBook.isEmpty());
assert(retBook.get() != null);
assertThat(retBook.isDefined()).isTrue();
assertThat(retBook).isNotEmpty();
}
@Test
@ -64,9 +65,11 @@ public class SpringTests {
testBook.setAuthors(authors);
JavaBook book = javaRepository.save(testBook);
java.util.List<JavaBook> books = javaRepository.findByTitleContaining("Seq");
assert(!books.isEmpty());
assert(books.size() == 1);
assert(books.get(0).getTitle().equals("Javaslang in Spring Data Seq Return"));
assertThat(books)
.isNotEmpty()
.hasSize(1)
.extracting("title")
.contains("Javaslang in Spring Data Seq Return");
}
@Test
@ -79,8 +82,8 @@ public class SpringTests {
testBook.setAuthors(authors);
JavaBook book = javaRepository.save(testBook);
JavaBook retBook = javaRepository.findById(1L);
assert(retBook != null);
assert(retBook.getId() == 1L);
assert(retBook.getTitle().contains("Data"));
assertThat(retBook.getId()).isEqualTo(1L);
assertThat(retBook.getTitle()).isEqualTo("Javaslang in Spring Data");
}
}

View File

@ -0,0 +1,21 @@
package baeldung.com;
import org.testng.Assert;
import org.testng.annotations.Test;
public class PriorityTest {
private String testString = "10";
private int testInt = 23;
@Test(priority = 1)
public void givenString_whenChangedToInt_thenCorrect() {
Assert.assertTrue(Integer.valueOf(testString) instanceof Integer);
}
@Test(priority = 2)
public void givenInt_whenChangedToString_thenCorrect() {
Assert.assertTrue(String.valueOf(testInt) instanceof String);
}
}