这是我的测试方法:
/content/
这是要测试的方法:
@RunWith(Parameterized.class)
@PrepareForTest(FilenameUtils.class)
public class Test {
//code
private final String datum;
private final String expectedResult;
public Test(String datum, String expectedResult){
this.datum = datum;
this.expectedResult = expectedResult;
}
@Parameters
public static Collection<Object[]> generateData(){
return Arrays.asList(new Object[][] {
{ ".jpg", "productimage..jpg" },
{ "jpg", "jpg.jpeg" },
{ "thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog.jpeg", "thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjump.jpeg" }
});
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testSanitizeFilename(){
PowerMock.mockStaticPartial(FilenameUtils.class, "getExtension" , "getBaseName");
expect(FilenameUtils.getExtension(datum)).andReturn("jpeg").anyTimes();
expect(FilenameUtils.getBaseName(datum)).andReturn("productimage").anyTimes();
PowerMock.replay(FilenameUtils.class);
String result = FileUtil.sanitizeFilename(datum, defaultName, contentType);
PowerMock.verify(FilenameUtils.class);
assertEquals(result, expectedResult);
}
}
该代码仅适用于FileUtil.sanitizeFilename中的条件不为true的情况。如果为真(通过传递文件名> 100个字符),则会发生以下错误:
public class FileUtil {
//code
public static String sanitizeFilename(String filename, ............) {
//code here
if (filename.length() > 100) {
FilenameUtils.getExtension(fileName);
FilenameUtils.getBaseName(fileName);
}
return fileName;
}
}
我的代码中有什么不正确?
修改 我使用了Parameterized类来避免for循环。
我使用PowerMock.mockStaticPartial作为静态方法getBaseName和getExtension。
答案 0 :(得分:1)
它对我有用吗
'N/A'
请注意,我修改了一些sanitizeFilename以获取工作代码。我用了
select DISTINCT NAME,
coalesce(GRP123.GRP, 'N/A') as 'GRP123', coalesce(CONVERT(VARCHAR, GRP123.START, 120), 'N/A') as 'GRP123START',
coalesce(GRPABC.GRP, 'N/A') as 'GRPABC', coalesce(CONVERT(VARCHAR, GRPABC.START, 120), 'N/A') as 'GRPABCSTART',
coalesce(GRPROMAN.GRP, 'N/A') as 'GRPROMAN', coalesce(CONVERT(VARCHAR, GRPROMAN.START, 120), 'N/A') as 'GRPROMANSTART'
from EMP
inner join (select * from GRP WHERE FINISH < '2000-01-01 00:00:00') as GRP_PIVOTING on EMP.ID = GRP_PIVOTING.ID
left join (select * from GRP WHERE GRP in ('GRP1', 'GRP2', 'GRP3') AND FINISH < '2000-01-01 00:00:00') as GRP123 on EMP.ID = GRP123.ID
left join (select * from GRP WHERE GRP in ('GRPA', 'GRPB', 'GRPC') AND FINISH < '2000-01-01 00:00:00') as GRPABC on EMP.ID = GRPABC.ID
left join (select * from GRP WHERE GRP in ('GRPI', 'GRPII', 'GRPIII', 'GRPIV', 'GRPV', 'GRPVI') AND FINISH < '2000-01-01 00:00:00') as GRPROMAN on EMP.ID = GRPROMAN.ID
where EMP.A = 'Y'
而不是@Test
public void testSanitizeFilename() {
PowerMock.mockStatic(FilenameUtils.class);
EasyMock.expect(FilenameUtils.getExtension(anyString())).andReturn("jpeg").anyTimes();
EasyMock.expect(FilenameUtils.getBaseName(anyString())).andReturn("jpeg").anyTimes();
PowerMock.replayAll();
for (Map.Entry<String, String> entry : fileUrls.entrySet()) {
System.out.println("KEY: " + entry.getKey());
String result = FileUtil.sanitizeFilename(entry.getKey(), entry.getValue());
assertEquals(result, entry.getValue());
PowerMock.verifyAll();
}
}
- 但是在此之前我已经将所有错误修复了。
看起来像在循环内部进行模拟并不是一个好主意 - 一旦我把模拟移出循环它就把我带到了某个地方。