U
U
user.2019-02-23 15:50:14
Java
user., 2019-02-23 15:50:14

Explain how TDD tests work in java using this class as an example?

From what I understand at the moment, first we write tests that roughly define our future program logic, then we write the logic itself ... if the logic satisfies our tests, then we see that the test passed .. and move on.
The name of the class and methods in which my class will be tested cannot be arbitrary?
When exactly will these tests be called?
After compiling my code, will the tests be called every time the program is started?
I use Intellij Community, do I need to install additional plugins or is there already something by default?
I would be glad if someone could explain in the simplest terms possible.

@Entity
@JsonInclude(value = Include.NON_EMPTY)
public class Post extends AuditableEntry {

  private String title;

  @OneToMany(mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  private List<Comment> comments;

  public Post() {

  }

  public Post(UUID id) {
    super(id);
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    Assert.hasText(title, "title cannot be null/empty/blank");
    this.title = title;
  }

  public List<Comment> getComments() {
    return comments;
  }

  @Override
  public int hashCode() {
    return super.hashCode();
  }

  @Override
  public boolean equals(Object obj) {
    return super.equals(obj);
  }

}

And the tests themselves:
public class PostTest {

  @Test
  public void shouldReturnItsValues() {
    UUID id = UUID.randomUUID();
    Post post = new Post(id);
    LocalDateTime createdAt = LocalDateTime.of(2018, 11, 22, 9, 30, 59);
    LocalDateTime updatedAt = LocalDateTime.of(2018, 11, 23, 10, 50, 01);
    post.setTitle("New post");		
    post.setContent("Bla bla");
    post.setCreatedAt(createdAt);
    post.setUpdatedAt(updatedAt);
    
    assertThat(post.getId()).isEqualTo(id);
    assertThat(post.getTitle()).isEqualTo("New post");
    assertThat(post.getContent()).isEqualTo("Bla bla");
    assertThat(post.getCreatedAt()).isEqualTo(createdAt);
    assertThat(post.getUpdatedAt()).isEqualTo(updatedAt);		
  }
  
  @Test()
  public void shouldNotAcceptNullEmptyNorBlankTitle() {
    Post post = new Post();
    Assertions.assertThatThrownBy(() -> post.setTitle(null)).isInstanceOf(IllegalArgumentException.class);
    Assertions.assertThatThrownBy(() -> post.setTitle("")).isInstanceOf(IllegalArgumentException.class);
    Assertions.assertThatThrownBy(() -> post.setTitle(" ")).isInstanceOf(IllegalArgumentException.class);
  }

  @Test()
  public void shouldNotAcceptNullEmptyNorBlankContent() {
    Post post = new Post();
    Assertions.assertThatThrownBy(() -> post.setContent(null)).isInstanceOf(IllegalArgumentException.class);
    Assertions.assertThatThrownBy(() -> post.setContent("")).isInstanceOf(IllegalArgumentException.class);
    Assertions.assertThatThrownBy(() -> post.setContent(" ")).isInstanceOf(IllegalArgumentException.class);
  }
  
  @Test
  public void shouldHonorBasicHashcodeContract() {
    UUID id = UUID.randomUUID();
    Post post = newPost(id);
    // Post has consistent hashcode
    assertThat(post.hashCode()).isEqualTo(post.hashCode());
    // 2 equal Post have same hashcode
    assertThat(newPost(id).hashCode()).isEqualTo(newPost(id).hashCode());
  }
  @Test
  public void shouldHonorBasicEqualsContract() {
    Post post = newPost();
    // equals to self
    assertThat(post.equals(post)).isTrue();
    // not equals to null
    assertThat(post.equals(null)).isFalse();
    // not equals to an instance of another class
    assertThat(post.equals(new Object())).isFalse();
  }

  @Test
  public void shouldBeEqualToAnotherPostWithSameNonNullID() {
    UUID id = UUID.randomUUID();
    UUID id2 = UUID.randomUUID();
    while (id2.equals(id))
      id2 = UUID.randomUUID();
    
    // 2 Post with null id are not equal
    assertThat(newPost().equals(newPost())).isFalse();
    // 2 Post with same id are equal
    assertThat(newPost(id).equals(newPost(id))).isTrue();
    // 2 Post with different id not are equal
    assertThat(newPost(id).equals(newPost(id2))).isFalse();
  }


  private Post newPost() {
    return new Post() {
    };
  }

  private Post newPost(UUID id) {
    return new Post(id) {
    };
  }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2019-02-25
@jaxtr

From what I understand at the moment, first we write tests that roughly define our future program logic, then we write the logic itself ... if the logic satisfies our tests, then we see that the test has passed .. and move on.

Yes, you understand correctly.
Whatever, but usually a class with tests is similar to the class under test, but with the Test suffix. For example: there is a class GreetingService, then the test class will be GreetingServiceTest
When building a project with build tools like Ant, Maven, or Gradle.
Tests are executed only when the project is built.
Support for JUnit or TestNG will require the appropriate plugins.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question