9월, 2019의 게시물 표시

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 테스트를 하려면 아래

Java Generic Type Erasure

Oracle 문서에 정확한 문구가 있어 발췌. Type Erasure Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. To implement generics, the Java compiler applies type erasure to: Replace all type parameters in generic types with their bounds or  Object  if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods. Insert type casts if necessary to preserve type safety. Generate bridge methods to preserve polymorphism in extended generic types. Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead. 출처:  https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

Covariant, Contravariant, Invariant 정의

Covariant / Contravariant / Invariant 정의 Covariant  : sub-type을 super-type으로 교체 가능한 성질. Contravariant  : super-type을 sub-type으로 교체 가능한 성질. Invariant  : 위 둘 모두가 허용되지 안는 성질. Java Array는 Covariant 하다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class CovariantTest { public static void main (String[] args) { Integer[] integers = new Integer[ 10 ]; for ( int i = 0 ; i < integers. length ; i++) { integers[i] = i; } // covariant: A feature which allows to substitute a subtype with supertype. // array is covariant. Number[] numbers = integers; for ( int i = 0 ; i < numbers. length ; i++) { System. out . println (numbers[i]); } } } Java Generic은 Invariant 하다. 1 2 3 4 5 6 7 8 9 10 11 12 class InvariantTest { public static void main (String[] args) { List<Integer> integerList = new ArrayList<>(); integerList. add (

How to bind @RequestParam to object in Spring

http://dolszewski.com/spring/how-to-bind-requestparam-to-object/ 아래 내용에 대한 설명이 잘 되어 있네요. 번역은 시간 될때... Controller의 @RequestParam을 object로 받는 방법. RequestParam object를 nested 하게 사용하는 방법. Multi ReqeustParam object 사용 방법. RequestParam object를 immutalbe하게 만드는 방법.

Java Heap Pollution 이란?

Heap Pollution Java generic type에서 변수화 된 타입이 가리키는 오브젝트의 타입이 해당 변수화 된 타입의 타입과 다를 때를 의미한다. 아래 예를 3라인을 보면 Integer로 type parameter가 정의 되었으나 4라인에서 이를 String으로 변경하여 heap pollution이 발생하였다. 컴파일시(javac) -Xlint:unchecked 옵션을 붙이면 unchecked warning 을 확인 할 수 있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class HeapPollutionDemo { public static void main ( String [] args ) { Set s = new TreeSet < Integer >(); Set < String > ss = s ; // unchecked warning s . add ( new Integer ( 42 )); // another unchecked warning Iterator < String > iter = ss . iterator (); while ( iter . hasNext ()) { String str = iter . next (); // ClassCastException thrown System . out . println ( str ); } } } 출처: https://en.m.wikipedia.org/wiki/Heap_pollution