Skip to content

Commit b9afb18

Browse files
Examples based on assertj-core 1.4.0 and assertions generator 1.1.0
1 parent b2f930d commit b9afb18

File tree

7 files changed

+107
-10
lines changed

7 files changed

+107
-10
lines changed

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
44

55
<modelVersion>4.0.0</modelVersion>
66
<groupId>org.assertj</groupId>
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>org.assertj</groupId>
3030
<artifactId>assertj-core</artifactId>
31-
<version>1.4.0-SNAPSHOT</version>
31+
<version>1.4.0</version>
3232
<scope>test</scope>
3333
</dependency>
3434
<dependency>
@@ -94,8 +94,14 @@
9494
<plugin>
9595
<groupId>org.assertj</groupId>
9696
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
97-
<version>1.1.0-SNAPSHOT</version>
98-
<!-- <executions> <execution> <goals> <goal>generate-assertions</goal> </goals> </execution> </executions> -->
97+
<version>1.1.0</version>
98+
<executions>
99+
<execution>
100+
<goals>
101+
<goal>generate-assertions</goal>
102+
</goals>
103+
</execution>
104+
</executions>
99105
<configuration>
100106
<packages>
101107
<param>org.assertj.examples.data</param>

src/main/java/org/assertj/examples/data/BasketBallPlayer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.assertj.examples.data;
1414

15+
import org.assertj.examples.exception.NameException;
16+
1517
import java.io.IOException;
1618

1719
import static java.lang.String.format;
@@ -28,14 +30,15 @@ public class BasketBallPlayer {
2830
private String team;
2931
private boolean rookie;
3032

31-
public BasketBallPlayer() {}
32-
3333
public BasketBallPlayer(Name name, String team) {
3434
setName(name);
3535
setTeam(team);
3636
}
3737

38-
public Name getName() throws IOException{
38+
public Name getName() throws NameException {
39+
if (name == null) {
40+
throw new NameException(name);
41+
}
3942
return name;
4043
}
4144

src/main/java/org/assertj/examples/data/Name.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,24 @@ public void setLast(String last) {
5151
@Override public String toString() {
5252
return String.format("%s[first=%s, last=%s]", getClass().getSimpleName(), first, last);
5353
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (this == o) return true;
58+
if (!(o instanceof Name)) return false;
59+
60+
Name name = (Name) o;
61+
62+
if (first != null ? !first.equals(name.first) : name.first != null) return false;
63+
if (last != null ? !last.equals(name.last) : name.last != null) return false;
64+
65+
return true;
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
int result = first != null ? first.hashCode() : 0;
71+
result = 31 * result + (last != null ? last.hashCode() : 0);
72+
return result;
73+
}
5474
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2013 the original author or authors.
12+
*/
13+
package org.assertj.examples.exception;
14+
15+
import org.assertj.examples.data.Name;
16+
17+
public class NameException extends Exception {
18+
public NameException(Name name) {
19+
super("Invalid name " + name);
20+
}
21+
}

src/test/java/org/assertj/examples/DateAssertionsExamples.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ public void is_in_the_same_moment_assertions_examples() {
8585

8686
Date date1 = parseDatetime("2003-04-26T13:01:02");
8787
Date date2 = parseDatetime("2003-04-26T13:01:03");
88-
// assertThat(date1).isInSameSecondAs(date2); // would fail since date1 and are date2 are not in the same second
88+
// would fail since date1 and are date2 are not in the same second
89+
try {
90+
assertThat(date1).isInSameSecondAs(date2);
91+
} catch (AssertionError e) {
92+
logAssertionErrorMessage("isInSameSecondAs date assertion", e);
93+
}
94+
8995
assertThat(date1).isInSameMinuteAs(date2);
9096
assertThat(date1).isInSameHourAs(date2);
9197
assertThat(date1).isInSameDayAs(date2);
@@ -108,6 +114,13 @@ public void is_within_date_assertions_examples() {
108114
assertThat(date1).isWithinDayOfWeek(Calendar.SATURDAY);
109115
assertThat(date1).isWithinMonth(4);
110116
assertThat(date1).isWithinYear(2003);
117+
// assertThat(date1).hasMillisecond(17);
118+
// assertThat(date1).hasSecond(35);
119+
// assertThat(date1).hasMinute(20);
120+
// assertThat(date1).hasHourOfDay(13);
121+
// assertThat(date1).hasDayOfWeek(Calendar.SATURDAY);
122+
// assertThat(date1).hasMonth(4);
123+
// assertThat(date1).hasYear(2003);
111124
}
112125

113126
@Test

src/test/java/org/assertj/examples/custom/CustomAssertExamples.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static org.assertj.examples.custom.MyProjectAssertions.assertThat;
1919

20+
import java.io.IOException;
2021
import java.util.List;
2122

2223
import org.junit.Test;
@@ -42,7 +43,7 @@ public void successful_custom_assertion_example() {
4243
// Assertions and add an assertThat(TolkienCharacter) that build a TolkienCharacterAssert, then import statically
4344
// MyProjectAssertions.assertThat to have a unique entry point to all assertions : yours and the core ones.
4445

45-
// For exmaple, the assertions below are accesed from MyProjectAssertions :
46+
// For example, the assertions below are accessed from MyProjectAssertions :
4647
// - hasName comes from .MyProjectAssertions.assertThat(TolkienCharacter actual)
4748
assertThat(frodo).hasName("Frodo").hasAge(33).isNotEqualTo(merry);
4849
// - isEqualTo is accessible since MyProjectAssertions inherits from Assertions which provides Integer assertions.
@@ -90,7 +91,7 @@ public void inherited_assertion_example() {
9091
}
9192

9293
@Test
93-
public void generics_limitation() {
94+
public void generics_limitation() throws IOException {
9495
Employee martin = new Employee("Martin Fowler");
9596
Employee kent = new Employee("Kent Beck");
9697
List<Employee> employees = newArrayList(martin, kent);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2013 the original author or authors.
12+
*/
13+
package org.assertj.examples.data;
14+
15+
import org.assertj.examples.AbstractAssertionsExamples;
16+
import org.assertj.examples.exception.NameException;
17+
import org.junit.Test;
18+
19+
/**
20+
*
21+
* Shows some example of a custom assertion class: {@link org.assertj.examples.custom.TolkienCharacterAssert} that
22+
* allows us to make assertions specific to {@link org.assertj.examples.data.TolkienCharacter}.
23+
*
24+
* @author Joel Costigliola
25+
*/
26+
public class BasketBallAssertExamples extends AbstractAssertionsExamples {
27+
28+
@Test
29+
public void basketBallPlayer_assertion_rethrow_getter_exception() throws NameException {
30+
// since BasketBallPlayer.getName throws NameException so does BasketBallPlayerAssert.hasName
31+
BasketBallPlayerAssert.assertThat(rose).hasName(new Name("Derrick", "Rose"));
32+
}
33+
}

0 commit comments

Comments
 (0)