我正在和Mockito(1.9.5)一起玩,并坚持第一个简单的测试用例:
List mockedList = mock(ArrayList.class);
assertEquals(0, mockedList.size()); // Passed
assertTrue(mockedList.isEmpty()); // Failed
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么isEmpty()在这里返回false而size()返回0?
我的课程实现方法如下:
void methodOne() {
try {
getHelper().doActionOne();
} catch ( Exception ex ) {
throw new CustomException( ex );
}
}
void methodTwo() {
try {
getHelper().doActionTwo();
} catch ( Exception ex ) {
throw new CustomException( ex );
}
}
void methodThree() {
try {
getHelper().doActionThree();
} catch ( Exception ex ) {
throw new CustomException( ex );
}
}
void methodFour;
void methodFive;
...
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?这些代码让我感到不舒服.
编辑: 抱歉不清楚的例子.我正在用Hibernate实现GenericDao类,真正的代码是这样的:
class GenericDaoImpl<T, PK> {
PK create( T object ) {
try …Run Code Online (Sandbox Code Playgroud) 我StackOverflowError在使用RegEx模式匹配结果时得到了.
模式是(\d\*?(;(?=\d))?)+.此正则表达式用于验证输入:
12345;4342;234*;123*;344324
输入是一个由分隔的值(仅数字)组成的字符串;.每个值最后可以包含一个*(用作其他匹配的通配符).;字符串的末尾没有.
问题是这个正则表达式工作正常,少数值.但是当值的数量太大(超过300)时,它将导致StackOverflowError.
final String TEST_REGEX = "(\\d\\*?(;(?=\\d))?)+";
// Generate string
StringBuilder builder = new StringBuilder();
int number = 123456;
for (int count = 1; count <= 300; count++) {
builder.append(Integer.toString(number).concat(";"));
number++;
}
builder.deleteCharAt(builder.lastIndexOf(";"))
builder.toString().matches(TEST_REGEX); //<---------- StackOverflowError
Run Code Online (Sandbox Code Playgroud)
和堆栈跟踪:
java.lang.StackOverflowError
at java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3715)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4556)
at java.util.regex.Pattern$Loop.match(Pattern.java:4683)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4615)
at java.util.regex.Pattern$Ques.match(Pattern.java:4079)
at java.util.regex.Pattern$Ques.match(Pattern.java:4079)
at java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3715)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4556)
at java.util.regex.Pattern$Loop.match(Pattern.java:4683)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4615)
at java.util.regex.Pattern$Ques.match(Pattern.java:4079)
at java.util.regex.Pattern$Ques.match(Pattern.java:4079)
at java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3715)
at …Run Code Online (Sandbox Code Playgroud) 最近我读了一些文章说有副作用的方法不好.所以我只想问一下我在这里的实现是否可归类为副作用.
假设我有一个SecurityGuard检查,看他是否应该允许客户去俱乐部.
该SecurityGuard要么只有validNames的列表或invalidNames的列表,而不是两个.
SecurityGuard只有validNames,他只允许名单上的客户.SecurityGuard只有invalidNames,他只允许名字不在列表中的客户.SecurityGuard根本没有列表,他允许每个人.因此,为了强制执行逻辑,在每个列表的setter上,如果新列表具有值,则重置其他列表.
class SecurityGaurd {
private List<String> validNames = new ArrayList<>();
private List<String> invalidNames = new ArrayList<>();
public void setValidNames(List<String> newValidNames) {
this.validNames = new ArrayList<>(newValidNames);
// empty the invalidNames if newValidNames has values
if (!this.validNames.isEmpty()) {
this.invalidNames = new ArrayList<>();
}
}
public void setInvalidNames(List<String> newInvalidNames) {
this.invalidNames = new ArrayList<>(newInvalidNames);
// empty the validNames if newInvalidNames has values
if (!this.invalidNames.isEmpty()) {
this.validNames = …Run Code Online (Sandbox Code Playgroud) 我一直致力于一个由其他开发人员开发的项目.在此项目中,任何返回实体或对象的方法都旨在返回一个名为的特殊值EMPTY_VALUE.
public Customer getCustomer() {
if (everythingFine) {
return realCustomer();
} else {
Customer.EMPTY_VALUE;
}
}
Run Code Online (Sandbox Code Playgroud)
而Customer类:
public class Customer {
public static final Customer EMPTY_VALUE = new Customer();
private String firstName;
private STring lastName;
public Customer() {
this.firstName = "";
this.lastName = "";
}
}
Run Code Online (Sandbox Code Playgroud)
在其他使用getCustomer()方法的地方:
Customer customer = getCustomer();
if (customer != Customer.EMPTY_VALUE) {
doSomething(customer);
}
Run Code Online (Sandbox Code Playgroud)
上述方法是否比null-checking 有任何优势?它给我们买了什么吗?
Customer customer = getCustomer();
if (customer != null) {
doSomething(customer);
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个批处理文件,用于查找并执行所有update.bat放在其上的目录中的所有文件.
这里的问题是我希望参数(即目录的路径)按名称排序,但事实证明它们按修改日期排序.
这是Windows(Windows 7)的默认行为吗?有什么建议可以解决这个问题?
这是我的批处理脚本:
@echo off
Setlocal EnableDelayedExpansion
if [%1]==[] goto :no_update_dropped
set LOG_FILE=update_log.txt
echo You are about to run these updates:
for %%G IN (%*) do (
if exist %%~sG\NUL echo %%G
)
pause
for %%G IN (%*) do (
if exist %%G\NUL (
if exist %%G\update.bat (
call %%G\update.bat %LOG_FILE%
) else (
echo No update.bat found in %%G.
goto :no_batch_found
)
)
)
goto :success
:no_update_dropped
echo NO UPDATE FOLDER FOUND
echo Drag and …Run Code Online (Sandbox Code Playgroud) 我实现了SubListIterator一个小实用程序,Iterator用于迭代给定列表的子列表.
假设我有一个包含13500个元素的List,我想将它拆分为7个子列表并使用它们.
@Test
public void shouldSplitTheGivenListIntoSmallerLists() {
List<Long> given = new ArrayList<Long>();
for (int count = 0; count < 13500; count++) {
given.add(Long.valueOf(count));
}
List<List<Long>> actualSubLists = new ArrayList<List<Long>>();
for (List<Long> subList : SubListIterator.subList(given, 2000)) { // Line got compilation error
actualSubLists.add(subList);
}
assertEquals(7, actualSubLists.size());
}
Run Code Online (Sandbox Code Playgroud)
如果我SubListIterator直接实现,一切都很好List<Long>.
然后我想延长我的SubListIterator每工作List,无论他们的泛型类型,所以我去改变List<Long>,以List<?>并获得编译错误:
Type mismatch: cannot convert from element type List<?> to List<Long>
我尝试过List<T>,它也不起作用.
我的问题是:无论如何,实现我的目标是让SubListIterator每个人都与之合作 …
java ×6
batch-file ×1
coding-style ×1
entity ×1
generics ×1
lookahead ×1
methods ×1
mocking ×1
mockito ×1
oop ×1
refactoring ×1
regex ×1