初始化时定义一个不可改变的Map集合
代码
package com
.zhaohp
.sofaboot
;
import org
.junit
.Test
;
import org
.junit
.runner
.RunWith
;
import org
.springframework
.boot
.test
.context
.SpringBootTest
;
import org
.springframework
.test
.context
.junit4
.SpringRunner
;
import java
.util
.Collections
;
import java
.util
.HashMap
;
import java
.util
.Map
;
@RunWith(SpringRunner
.class)
@SpringBootTest(classes
= SofabootApplication
.class, webEnvironment
= SpringBootTest
.WebEnvironment
.RANDOM_PORT
)
public class SofabootApplicationTests {
private static Map
<Integer, String> map
= new HashMap<Integer, String>();
static {
map
.put(1, "one");
map
.put(2, "two");
map
= Collections
.unmodifiableMap(map
);
}
@Test
public void test1() {
try {
map
.put(1, "three");
} catch (UnsupportedOperationException e
) {
System
.out
.println("This Map cannot be modified.");
e
.printStackTrace();
}
}
}
异常
java.lang.UnsupportedOperationException
转载请注明原文地址: https://yun.8miu.com/read-54703.html