작성
·
442
0
jpa와 db설정, 동작확인 강의를 듣고 클론코딩 중
test중 NullPointerException에러발생
main
jpabook.jpashop.Member
package jpabook.jpashop;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;
@Entity
@Getter @Setter
public class Member {
@Id @GeneratedValue
private Long id;
private String username;
}
jpabook.jpashop.MemberRepository
package jpabook.jpashop;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
@Repository
public class MemberRepository {
@PersistenceContext
EntityManager em;
public Long save(Member member) {
em.persist(member);
return member.getId();
}
public Member find(Long id) {
return em.find(Member.class, id);
}
}
test
jpabook.jpashop.MemberRepositoryTest
package jpabook.jpashop;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
public class MemberRepositoryTest {
@Autowired
MemberRepository memberRepository;
@Test
@Transactional
@Rollback(false)
public void testMember() {
Member member = new Member();
member.setUsername("memberA");
Long saveId = memberRepository.save(member);
Member findMember = memberRepository.find(saveId);
assertThat(findMember.getId()).isEqualTo(member.getId());
assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
}
}
application.yml
spring:
datasource:
url: jdbc:h2:tcp://localhost/~/jpashop
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
# show_sql: true
format_sql: true
logging.level:
org.hibernate.SQL: debug
org.hibernate.type: trace
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'jpabook'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
build, run 모두 intellj IDE로 하는중입니다.
답변 3
0
안녕하세요, 인프런 AI 인턴입니다.
질문하신 내용과 유사한 문제를 겪고 계신 다른 수강생의 질문과 강사님의 답변이 있는 게시글을 찾았습니다. NullPointerException 오류의 경우, 보통 빈(Bean)이 제대로 주입되지 않았을 때 발생할 수 있습니다.
수강생님이 제시한 코드를 바탕으로 테스트 중 발생하는 NullPointerException
오류를 해결하기 위한 정보를 아래 게시글에서 확인하실 수 있습니다.
수강생님의 상황과 유사한 문제를 다루고 있으니, 링크를 통해 답변 내용을 확인해 보시기 바랍니다.
감사합니다.
저랑 에러가 다른데요 ..ㅠ