인프런 커뮤니티 질문&답변

blossom_mind님의 프로필 이미지
blossom_mind

작성한 질문수

실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발

회원 기능 테스트

테스트 에러 질문드립니다.

작성

·

1.2K

0

package com.service.com.service;

import static org.junit.Assert.assertEquals;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

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 com.service.com.domain.Member;
import com.service.com.repository.MemberRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class MemberServiceTest {
	@Autowired
	MemberService memberService; 
	@Autowired
	MemberRepository memberRepository;
	@Autowired
	EntityManager em;

	@Test
	public void 회원가입() throws Exception{
		Member member = new Member();
		member.setName("Kim");

		// when
		Long saveId = memberService.join(member);

		// then
		assertEquals(member, memberRepository.findOne(saveId));
	}

	
	
	
	@Test(expected = IllegalStateException.class)
	public void 중복_회원_예외() throws Exception
	{
		Member member1 = new Member();
		member1.setName("kim");

		Member member2 = new Member();
		member2.setName("kim");

				fail("예외가 발생해야 한다");

	}
}


2023-11-16 20:00:08.269 INFO 22664 --- [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@1c3b9394 testClass = MemberServiceTest, testInstance = com.service.com.service.MemberServiceTest@5cc152f9, testMethod = 중복_회원_예외@MemberServiceTest, testException = java.lang.AssertionError: Expected exception: java.lang.IllegalStateException, mergedContextConfiguration = [WebMergedContextConfiguration@6f2cfcc2 testClass = MemberServiceTest, locations = '{}', classes = '{class com.service.com.JpashopApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4310d43, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@3527942a, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4f80542f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@576d5deb, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2794eab6, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@3972a855], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]

java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.getAncestors()Ljava/util/Set;

at org.junit.platform.launcher.core.StackTracePruningEngineExecutionListener.getTestClassNames(StackTracePruningEngineExecutionListener.java:50)

at org.junit.platform.launcher.core.StackTracePruningEngineExecutionListener.executionFinished(StackTracePruningEngineExecutionListener.java:39)

at org.junit.platform.launcher.core.DelegatingEngineExecutionListener.executionFinished(DelegatingEngineExecutionListener.java:46)

at org.junit.platform.launcher.core.OutcomeDelayingEngineExecutionListener.reportEngineFailure(OutcomeDelayingEngineExecutionListener.java:83)

at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:203)

at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:169)

at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:93)

at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:58)

at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:141)

at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:57)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:103)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:94)

at org.junit.platform.launcher.core.DelegatingLauncher.execute(DelegatingLauncher.java:52)

at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:70)

at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)



plugins {

id 'java'

id 'org.springframework.boot' version '2.7.17'

id 'io.spring.dependency-management' version '1.0.15.RELEASE'

}

 

group = 'com.example'

version = '0.0.1-SNAPSHOT'

 

java {

sourceCompatibility = '11'

}

 

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.projectlombok:lombok'

runtimeOnly 'com.h2database:h2'

developmentOnly 'org.springframework.boot:spring-boot-devtools'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

//JUnit4 추가

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'

testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

 

tasks.named('bootBuildImage') {

builder = 'paketobuildpacks/builder-jammy-base:latest'

}

 

tasks.named('test') {

useJUnitPlatform()

}

이거 어디가 잘못된걸까요 예제 파일 다운받아서 4로 해도 마찬가지고 5로 해도 마찬가지로 나오네요 ..

답변 1

0

안녕하세요. blossom_mind님, 공식 서포터즈 OMG입니다.

fail()을 호출하기 위한 Import문이 보이지 않네요.

강의에서는

import static org.junit.Assert.*;

해당 import로 fail() 사용이 가능했는데요,

 

 

image

올리신 코드에서 보면

image

import static으로 assertEquals만 호출이 가능하겠네요 ^^

변경하여 확인해주세요 :)

 

감사합니다.

blossom_mind님의 프로필 이미지
blossom_mind

작성한 질문수

질문하기