POJO에게 인 키에서 맵의 값을 얻는 방법나는 다음과 같은 기능을 가지고
Map<MyClass, String> someFunction() {
Map<MyClass, String> result = new HashMap<>();
return result.put(new MyClass("someString"), "someOtherString"));
}
MyClass에의 구현은 다음과 같습니다
: 내 테스트 I에서
public class MyClass{
String string;
public MyClass(String string) {
this.string = string;
}
public void setString(String string) {
this.string = string;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((string== null) ? 0 : string.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
MyClass other = (MyClass) obj;
if (string== null) {
if (other.string!= null) {
return false;
}
} else if (!string.equals(other.string)) {
return false;
}
return true;
}
}
다음을 수행합니다.
@Test
public void test() {
Map<MyClass, String> outcome = classUnderTest.someFunction();
assertThat(outcome.get(new MyClass("someString")), is("someOtherString"));
}
그러나이 테스트는 실패합니다. 왜냐하면 actual가 null이기 때문입니다. 나는 다음과 같은 시도 할 경우 :
assertThat(outcome.keySet(), hasItem(MY_CLASS));
이 이러한 다른 intantiations 것을, 말해, 실패합니다. 나는 심지어 내 테스트를 디버그하려했지만 equals 메소드에 도달하지 못했다. 여기서 무슨 일이 일어 났는지 말해 줄 수 있니?) (
공공 V 넣어 (K 키, V 값)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Returns:
the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)
넣어 반환 : 함수에서
junit의 어떤 버전을 사용하고 있습니까? – Eben
현재 버전 4.11을 사용 중입니다. – Chris311