gpt4 book ai didi

java - 使用 junit 和 PowerMockito 在单元测试中模拟文件操作不起作用

转载 作者:行者123 更新时间:2023-11-30 07:47:05 25 4
gpt4 key购买 nike

我正在尝试为包含文件操作的静态方法编写单元测试。我正在使用 junit 和 PowerMockito。要测试的方法是将csv文件转换为bean列表。由于这是单元测试,我模拟了我们方法内部的方法调用。但出现以下错误,

org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object.

at com.mypackage..unittest.UTCSVBeanUtil.convert_convertingCsvToBean(UTCSVBeanUtil.java:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

我的类(class)是,

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;

public class CSVBeanUtil {

public static <T> List<T> fileToBean(final String filename, final char delimiter, final Class<T> beanClass,
final String[] columns) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
try {
return bufferReaderToBean(reader, delimiter, beanClass, columns);
} finally {
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
// ignore
}
}
}
}

public static <T> List<T> stringToBean() {
return null;
}

public static <T> List<T> bufferReaderToBean(BufferedReader reader, final char delimiter, final Class<T> beanClass,
final String[] columns) {
CSVReader csvreader = null;
final CsvToBean<T> csv = new CsvToBean<T>();
csvreader = new CSVReader(reader, delimiter);
ColumnPositionMappingStrategy<T> strategy = new ColumnPositionMappingStrategy<T>();
strategy.setType(beanClass);
strategy.setColumnMapping(columns);
return csv.parse(strategy, csvreader);
}

public static boolean writeToCsv(List<String[]> beanList, Path absPath) throws IOException {
CSVWriter writer = new CSVWriter(new FileWriter(absPath.toAbsolutePath().toString()),
CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
writer.writeAll(beanList);
writer.close();
return false;
}

}

我的测试类是,

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import com.mypackage..config.AppConfig;
import com.mypackage..entity.MyFile;
import com.mypackage..service.MyFileValidation;
import com.mypackage..utility.CSVBeanUtil;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@WebAppConfiguration
@ContextConfiguration(classes = { AppConfig.class })
@PrepareForTest(CSVBeanUtil.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class UTCSVBeanUtil {
List<MyFile> dataList = new ArrayList<MyFile>();
List<MyFile> expectedList=new ArrayList<MyFile>();

@Before
public void beforeClass() {
expectedList = getdataList();
}

@Test
public void convert_convertingCsvToBean()
throws IOException{
PowerMockito.mockStatic(CSVBeanUtil.class);
// BufferedReader bufferedReader = Mockito.mock(BufferedReader.class); // while using this the test execution doesn't terminate
BufferedReader bufferedReader= new BufferedReader(new StringReader("201030"));
// File file = Mockito.mock(File.class);
Mockito.when(CSVBeanUtil.bufferReaderToBean(bufferedReader, ',', MyFile.class, MyFile.columns))
.thenReturn(expectedList);
dataList.addAll(CSVBeanUtil.fileToBean( null, ',', MyFile.class, MyFile.columns));
assertEquals(expectedList,dataList);
}

private List<MyFile> getdataList() {
List<MyFile> expectedList=new ArrayList<MyFile>();
MyFile gv=new MyFile();
gv.setTRADENUM("201030");
expectedList.add(gv);
return expectedList;
}

}

我正在努力解决这个问题。请帮助我...谢谢

最佳答案

PowerMock 提供了自己的运行器,使用 @RunWith 注释指定

@RunWith( PowerMockRunner.class)

如果你想使用SpringJUnit4ClassRunner.class

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)

Using another JUnit Runner with PowerMock

关于java - 使用 junit 和 PowerMockito 在单元测试中模拟文件操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33773696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com