Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
		
						commit
						2f627a0bab
					
				
							
								
								
									
										38
									
								
								assertj/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								assertj/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | ||||
| <?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>com.baeldung</groupId> | ||||
|     <artifactId>assertj-introduction</artifactId> | ||||
|     <version>1.0.0-SNAPSHOT</version> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>junit</groupId> | ||||
|             <artifactId>junit</artifactId> | ||||
|             <version>4.12</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.assertj</groupId> | ||||
|             <artifactId>assertj-core</artifactId> | ||||
|             <version>3.4.1</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <build> | ||||
|         <plugins> | ||||
|             <plugin> | ||||
|                 <groupId>org.apache.maven.plugins</groupId> | ||||
|                 <artifactId>maven-compiler-plugin</artifactId> | ||||
|                 <version>3.3</version> | ||||
|                 <configuration> | ||||
|                     <source>1.8</source> | ||||
|                     <target>1.8</target> | ||||
|                 </configuration> | ||||
|             </plugin> | ||||
|         </plugins> | ||||
|     </build> | ||||
| </project> | ||||
| @ -0,0 +1,19 @@ | ||||
| package com.baeldung.assertj.introduction.domain; | ||||
| 
 | ||||
| public class Dog { | ||||
|     private String name; | ||||
|     private Float weight; | ||||
| 
 | ||||
|     public Dog(String name, Float weight) { | ||||
|         this.name = name; | ||||
|         this.weight = weight; | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public Float getWeight() { | ||||
|         return weight; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,19 @@ | ||||
| package com.baeldung.assertj.introduction.domain; | ||||
| 
 | ||||
| public class Person { | ||||
|     private String name; | ||||
|     private Integer age; | ||||
| 
 | ||||
|     public Person(String name, Integer age) { | ||||
|         this.name = name; | ||||
|         this.age = age; | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public Integer getAge() { | ||||
|         return age; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,77 @@ | ||||
| package com.baeldung.assertj.introduction; | ||||
| 
 | ||||
| import com.baeldung.assertj.introduction.domain.Dog; | ||||
| import com.baeldung.assertj.introduction.domain.Person; | ||||
| import org.junit.Ignore; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.NoSuchElementException; | ||||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| import static org.assertj.core.api.Assertions.withPrecision; | ||||
| 
 | ||||
| public class AssertJDemoTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenComparingReferences_thenNotEqual() throws Exception { | ||||
|         Dog fido = new Dog("Fido", 5.15f); | ||||
|         Dog fidosClone = new Dog("Fido", 5.15f); | ||||
| 
 | ||||
|         assertThat(fido).isNotEqualTo(fidosClone); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenComparingFields_thenEqual() throws Exception { | ||||
|         Dog fido = new Dog("Fido", 5.15f); | ||||
|         Dog fidosClone = new Dog("Fido", 5.15f); | ||||
| 
 | ||||
|         assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCheckingForElement_thenContains() throws Exception { | ||||
|         List<String> list = Arrays.asList("1", "2", "3"); | ||||
| 
 | ||||
|         assertThat(list) | ||||
|                 .contains("1"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCheckingForElement_thenMultipleAssertions() throws Exception { | ||||
|         List<String> list = Arrays.asList("1", "2", "3"); | ||||
| 
 | ||||
|         assertThat(list) | ||||
|                 .isNotEmpty() | ||||
|                 .contains("1") | ||||
|                 .startsWith("1") | ||||
|                 .doesNotContainNull() | ||||
|                 .containsSequence("2", "3"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCheckingRunnable_thenIsInterface() throws Exception { | ||||
|         assertThat(Runnable.class).isInterface(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { | ||||
|         assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenComparingWithOffset_thenEquals() throws Exception { | ||||
|         assertThat(5.1).isEqualTo(5, withPrecision(1d)); | ||||
|     } | ||||
| 
 | ||||
|     @Ignore // IN ORDER TO TEST, REMOVE THIS LINE | ||||
|     @Test | ||||
|     public void whenRunningAssertion_thenDescribed() throws Exception { | ||||
|         Person person = new Person("Alex", 34); | ||||
| 
 | ||||
|         assertThat(person.getAge()) | ||||
|                 .as("%s's age should be equal to 100") | ||||
|                 .isEqualTo(100); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user