라벨이 junit인 게시물 표시

[IntelliJ] Test events were not received

이미지
IntelliJ에서 JUnit 테스트시 아래와 같은 오류 메세지가 나올 때 해결 방법 Test events were not received 1. Preference > Build, Execution, Deployment > Build Tools > Gradle 로 이동 2. Run tests using 을 IntelliJ IDEA 로 설정

JUnit Mock Object Equality

controller JUnit 테스트를 위해 service를 mocking 하여 테스트시 equals 관련 문제를 겪은 적이 있다. 우선 아래와 같은 코드가 있다고 할때, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // RestController @GetMapping (value = "/test/{id}/info" ) public TestInfo getTestInfo ( @Valid RequestCommand command) { return TestInfoService. getTestInfo (command); } // Service public TestInfo getTestInfo (RequestCommand command) { // ... command에 따른 로직 return new TestInfo ( "test" ); } // Command class public class RequestCommand { private String id; // getters & setters } // Domain class public class TestInfo { private String str; public TestInfo (String str) { this . str = str; } // getters & setters } 설명) controller는 path variable로 id를 RequestCommand 객체로 받는다. 이 command를 DTO로 service로 넘겨 연산을 하고 entity를 받는다. 받은 entity를 json으로 응답한다. 이 controller의 service를 mocking 하여 Unit 테스트를 하려면 아래...

[Mockito] Argument Matchers – any(), eq()

Mockito는 mock 오브젝트를 생성하거나, behavior 테스트 케이스를 작성할 수 있는 stub을 생성 할 수 있도록 해 준다. 일반적으로는 behavior 테스트 mock을 생성할때  when()  과  thenReturn()  을 사용 한다. Table of Contents  [ hide ] 1  Mockito Argument Matchers – any() 2  Mockito Argument Matcher – eq() 3  Mockito AdditionalMatchers 4  Mockito Verify Argument Matchers 5  Summary Mockito Argument Matchers – any() 이따금, behavior mock을 작성할때 함수의 argument type으로 아무 type이나 주고 싶을 때가 있는데 이럴때 argument matchers를 사용하면 될것이다. Mockito argument methods 는  org.mockito.ArgumentMatchers 클래스에 static method로 정의 되어 있다. 아래 method들이 있을 때, class Foo { boolean bool (String str, int i, Object obj) { return false ; } int in ( boolean b, List<String> strs) { return 0 ; } int bar ( byte [] bytes, String[] s, int i) { return 0 ; } } arguments를 generic 하게 쓰기 위해 아래처럼 mockito argument matchers를 사용할 수 있다. 주의할 점은 matchers를 사용하게 되면 모든 argument에 다 쓰던지 아니면 안쓰던지 해야지 argument...

[Java] Spring Boot Test

Spring Boot에서 테스트를 - 1 Spring Boot는 애플리케이션을 테스트하기 위한 많은 유틸 어노테이션을 제공합니다. Spring boot에서 테스트 모듈dms spring-boot-test와 spring-boot-test-autoconfigure가 존재합니다. 대부분의 경우는 spring-boot-starter-test만으로도 충분합니다. spring-boot-starter-test는 spring boot의 테스트에 사용되는 Starter 패키지입니다. spring-boot-starter-test는 JUnit는 물론이고, AssertJ, Hamcrest를 포함한 여러 유용한 라이브러리를 포함하고 있습니다. 포함되어있는 주요 라이브러리들 기존 Spring framework에서 사용하던 spring-test 이외에도 여러 유용한 라이브러리를 포함하고 있습니다. 가지고 있는 라이브러리에는 Mocktio도 포함되어있는데 기본적으로 Mocktio 1.x 버전을 사용합니다. 하지만 원한다면 2.x 버전를 사용하는 것도 가능합니다. JUnit Spring Test & Spring Boot Test AssertJ Hamcrest Mockito JSONassert JsonPath @SpringBootTest 어노테이션 spring-boot-test는 @SpringBootTest라는 어노테이션을 제공합니다. 이 어노테이션을 사용하면 테스트에 사용할 ApplicationContext를 쉽게 생성하고 조작할 수 있습니다. 기존 spring-test에서 사용하던 @ContextConfiguration의 발전된 기능이라고 할 수 있습니다. @SpringBootTest는 매우 다양한 기능을 제공합니다. 전체 빈 중 특정 빈을 선택하여 생성한다던지, 특정 빈을 Mock으로 대체한다던지, 테스트에 사용할 프로퍼티 파일을 선택하거나 특정 속성만 추가한다던지, 특정 Configuration을 선택하여 설정할 수도 있습니다. 또한, 주요 ...