인프런 커뮤니티 질문&답변
테스트가 안됩니다 @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
해결된 질문
작성
·
179
0
안녕하세요 강의 잘 듣고 있습니다. 실전 Querydsl에서 설정 후 테스트 중
테스트가 잘 안되서 질문 드립니다.
package study.querydsl;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Commit;
import org.springframework.transaction.annotation.Transactional;
import study.querydsl.entity.Hello;
import study.querydsl.entity.QHello;
import javax.persistence.EntityManager;
@SpringBootTest
@Transactional
class QuerydslApplicationTests {
@Autowired
EntityManager em;
@Test
void contextLoads() {
Hello hello = new Hello();
em.persist(hello);
JPAQueryFactory query = new JPAQueryFactory(em);
QHello qHello = QHello.hello; //Querydsl Q타입 동작 확인
Hello result = query
.selectFrom(qHello)
.fetchOne();
Assertions.assertThat(result).isEqualTo(hello);
//lombok 동작 확인 (hello.getId())
Assertions.assertThat(result.getId()).isEqualTo(hello.getId());
}
}
--오류메세지--
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
at org.springframework.util.Assert.state(Assert.java:76)
테스트 코드와 실제 코드 디렉토리때문인가 해서 @SpringBootTest(classes=Hello.class) 로 실행하면
em bean관련 오류코드가 뜹니다..
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'study.querydsl.QuerydslApplicationTests': Unsatisfied dependency expressed through field 'em'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
답변




