我们都知道你不能这样做:
for (Object i : l) {
if (condition(i)) {
l.remove(i);
}
}
Run Code Online (Sandbox Code Playgroud)
ConcurrentModificationException等等......这显然有时起作用,但并非总是如此.这是一些特定的代码:
public static void main(String[] args) {
Collection<Integer> l = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
l.add(4);
l.add(5);
l.add(6);
}
for (int i : l) {
if (i == 5) {
l.remove(i);
}
}
System.out.println(l);
}
Run Code Online (Sandbox Code Playgroud)
当然,这会导致:
Exception in thread "main" java.util.ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud)
...即使多线程没有这样做......无论如何.
什么是这个问题的最佳解决方案?如何在循环中从集合中删除项而不抛出此异常?
我也在Collection这里使用任意,不一定是ArrayList,所以你不能依赖get.
如何用void返回类型模拟方法?
我实现了一个观察者模式,但我无法用Mockito模拟它,因为我不知道如何.
我试图在互联网上找到一个例子,但没有成功.
我的班级看起来像
public class World {
List<Listener> listeners;
void addListener(Listener item) {
listeners.add(item);
}
void doAction(Action goal,Object obj) {
setState("i received");
goal.doAction(obj);
setState("i finished");
}
private string state;
//setter getter state
}
public class WorldTest implements Listener {
@Test public void word{
World w= mock(World.class);
w.addListener(this);
...
...
}
}
interface Listener {
void doAction();
}
Run Code Online (Sandbox Code Playgroud)
系统不会通过模拟触发.=(我想显示上面提到的系统状态.并根据它们做出断言.
有没有办法,使用Mockito来模拟一个类中的某些方法,而不是其他方法?
例如,在这个(公认的设计)Stock类中我想模拟getPrice()和getQuantity()返回值(如下面的测试片段所示),但我希望getValue()执行在Stock中编码的乘法类
public class Stock {
private final double price;
private final int quantity;
Stock(double price, int quantity) {
this.price = price;
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public double getValue() {
return getPrice() * getQuantity();
}
@Test
public void getValueTest() {
Stock stock = mock(Stock.class);
when(stock.getPrice()).thenReturn(100.00);
when(stock.getQuantity()).thenReturn(200);
double value = stock.getValue();
// Unfortunately the following assert fails, because the mock Stock getValue() method does not perform the …Run Code Online (Sandbox Code Playgroud) 是否可以为提交后提交期间受影响的所有文件创建补丁(差异文件)?这些文件具有相同的版本号,我需要来自其先前版本的每个文件的差异.
我有一个使用Jersey构建的REST服务,并部署在AppEngine中.REST服务实现使用application/json媒体类型的动词PUT.数据绑定由Jackson执行.
动词使用JSON表示的企业部门关系
{"name":"myEnterprise", "departments":["HR","IT","SC"]}
Run Code Online (Sandbox Code Playgroud)
在客户端,我使用gson将JSON表示转换为java对象.然后,我将对象传递给我的REST服务,它工作正常.
问题:
当我的JSON表示在集合中只有一个项目时
{"name":"myEnterprise", "departments":["HR"]}
Run Code Online (Sandbox Code Playgroud)
该服务无法反序列化该对象.
ATTENTION: /enterprise/enterprise: org.codehaus.jackson.map.JsonMappingException:
Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token at
[Source: org.mortbay.jetty.HttpParser$Input@5a9c5842; line: 1, column: 2
Run Code Online (Sandbox Code Playgroud)
正如其他用户所报告的那样,解决方案是添加标志ACCEPT_SINGLE_VALUE_AS_ARRAY(例如,Jersey:无法从String中反序列化ArrayList的实例).然而,我并不是在控制ObjectMapper,因为在服务方面它是由Jackson透明地制作的.
题:
有没有办法在服务端配置ObjectMapper以启用ACCEPT_SINGLE_VALUE_AS_ARRAY?注释?web.xml中?
代码细节
Java对象:
@XmlRootElement
public class Enterprise {
private String name;
private List<String> departments;
public Enterprise() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getDepartments() {
return departments;
}
public void setDepartments(List<String> departments) {
this.departments …Run Code Online (Sandbox Code Playgroud) 我遇到了一个@Autowired的例子
public class EmpManager {
@Autowired
private EmpDao empDao;
}
Run Code Online (Sandbox Code Playgroud)
我很好奇empDao如何得到集合,因为没有setter方法而且它是私有的.
在Intellij中,如果我在源文件中有错误,那么在构建时我可以"Exclude from Compile"通过右键单击构建输出中的文件来选择.但是,现在我想再次包含该文件.菜单项在哪里让我把它放回到构建中?(该文件显示在"项目"窗口中,并且'x'其图标中有一个小图标以显示它已被排除,但我认为没有办法让它消失.)
编辑 - 我发现有一个compiler.xml文件,我发现我可以从该"excludeFromCompile"部分删除该文件,但仍然必须有一个菜单项来执行此操作.
我想测试依赖于使用@Autowired和@ConfigurationProperties加载的属性的应用程序的一小部分.我正在寻找一个只加载所需属性的解决方案,而不是整个ApplicationContext.这里以简化为例:
@TestPropertySource(locations = "/SettingsTest.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestSettings.class, TestConfiguration.class})
public class SettingsTest {
@Autowired
TestConfiguration config;
@Test
public void testConfig(){
Assert.assertEquals("TEST_PROPERTY", config.settings().getProperty());
}
}
Run Code Online (Sandbox Code Playgroud)
配置类:
public class TestConfiguration {
@Bean
@ConfigurationProperties(prefix = "test")
public TestSettings settings (){
return new TestSettings();
}
}
Run Code Online (Sandbox Code Playgroud)
设置类:
public class TestSettings {
private String property;
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
Run Code Online (Sandbox Code Playgroud)
资源文件夹中的属性文件包含以下条目:
test.property=TEST_PROPERTY
Run Code Online (Sandbox Code Playgroud)
在我当前的设置配置不是null,但没有可用的字段.字段不是字段的原因应该与我不使用Springboot但是Spring的事实有关.那么Springboot的运行方式是什么呢?
编辑: 我想这样做的原因是:我有一个解析Textfiles的解析器,使用的正则表达式存储在属性文件中.为了测试这个,我想只加载这个解析器所需的属性,它们位于TestSettings之上的exaple中.
在阅读评论时,我已经注意到这不再是单元测试了.但是,对于这个小测试使用完整的Spring启动配置对我来说似乎有点太多了.这就是为什么我问是否有可能只加载一个属性的类.
我有以下DTO:
@Value
public class PracticeResults {
@NotNull
Map<Long, Boolean> wordAnswers;
}
@Value
public class ProfileMetaDto {
@NotEmpty
String name;
@Email
String email;
@Size(min = 5)
String password;
}
Run Code Online (Sandbox Code Playgroud)
@Value是一个生成构造函数的Lombok注释.这意味着该类没有no-arg构造函数.
我使用Spring Boot 1.4.3.RELEASE和ObjectMapper bean能够从json反序列化这样的对象.
升级到Spring Boot 2.0.0.M7后,我收到以下异常:
@Value
Spring Boot 1.4.3中使用的Jackson版本适用于ObjectMapperSpring Boot 2.0.0.M7是com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of PracticeResults (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator).
我试过谷歌这个问题,但发现只有2.8.10或的解决方案2.9.2.
那么,为什么它与Spring Boot 1.4.3一起使用并且在Spring Boot 2中失败?是否可以将bean配置为与旧版本相同的方式?
java ×6
jackson ×2
mocking ×2
mockito ×2
spring ×2
spring-boot ×2
arraylist ×1
autowired ×1
collections ×1
cursor ×1
diff ×1
editor ×1
iteration ×1
json ×1
lombok ×1
properties ×1
svn ×1
unit-testing ×1
void ×1