관리 메뉴

bread, coffee and coding

Springboot 2일차 본문

Springboot

Springboot 2일차

DongJin lee 2021. 7. 13. 14:48

 

 

package com.jojoldu.book.springboot.domain.posts;


import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Getter
@NoArgsConstructor
@Entity
public class Posts {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(length = 500, nullable = false)
    private String title;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String content;

    private String author;

    @Builder
    public Posts(String title, String content, String author) {
        this.title = title;
        this.content = content;
        this.author = author;
    }
}

Posts.java

 

 


 

package com.jojoldu.book.springboot.domain.posts;

import org.springframework.data.jpa.repository.JpaRepository;
public interface PostsRepository extends  JpaRepository<Posts, Long> {
}

PostsRepository.java

 


package com.jojoldu.book.springboot.web.domain.posts;
import com.jojoldu.book.springboot.domain.posts.Posts;
import com.jojoldu.book.springboot.domain.posts.PostsRepository;
import org.junit.After;
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.test.context.junit4.SpringRunner;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PostsRepositoryTest {

    @Autowired
    PostsRepository postsRepository;

    @After
    public  void cleanup(){
        postsRepository.deleteAll();
    }

    @Test
    public void postsave_loading() {
        String title = "testpost";
        String content = "testmaintext";

        postsRepository.save(Posts.builder().title(title)
                                            .content(content)
                                            .author("jojoldu@gmail.com")
                                            .build());

        List<Posts> postsList = postsRepository.findAll();

        Posts posts = postsList.get(0);
        assertThat(posts.getTitle()).isEqualTo(title);
        assertThat(posts.getContent()).isEqualTo(content);
    }
}

PostsReponsitoryTest.java

 


 

'Springboot' 카테고리의 다른 글

Spring Boot 2.0  (0) 2021.07.22
Spirngboot D-3  (0) 2021.07.16
Springboot 1일차  (0) 2021.07.12