为什么在Java类字段中将其初始化为null/0而局部变量不是?这种语言设计选择有原因吗?
当我尝试在单元测试中窥探一个对象时,我得到了一个例外.这是我的单元测试文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/applicationContext.xml" })
public class BookingSuperManTest {
BookInfoParams bookInfoParams;
HttpAttributeParams httpAttributeParams;
AbstractRequester requester;
public void beforeStartTest(){
bookInfoParams = Mockito.spy(new BookInfoParams());
httpAttributeParams = Mockito.spy(new HttpAttributeParams());
}
@Test
public void step1GoToHomePage() throws BookingException{
beforeStartTest();
requester = new Step1HomePage(bookInfoParams, httpAttributeParams);
requester.executeRequest();
Assert.assertNotNull(httpAttributeParams.getResponseGetRequest());
}
}
Run Code Online (Sandbox Code Playgroud)
我在链接分配bookInfoParams spy时得到了异常:
java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor
at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:379)
at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:61)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:13)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:12)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:23)
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:1103)
at ive.core.test.webbot.book.vietjet.BookingSuperManTest.beforeStartTest(BookingSuperManTest.java:46)
at ive.core.test.webbot.book.vietjet.BookingSuperManTest.step1GoToHomePage(BookingSuperManTest.java:54) …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Spring MVC 的项目。我正在尝试为项目架构中的服务模块编写单元测试。所有服务类都从名为“BaseService”的超类扩展。基础服务是这样的:
public abstract class BaseService {
private static final Logger logger = LoggerFactory.getLogger(BaseService.class);
@Autowired(required = true)
private HttpServletRequest request;
@Autowired
private ReloadableResourceBundleMessageSource messageSource;
/*
* Injecting Mapper
*/
@Resource
private Mapper mapper;
...
public <T extends BaseBVO, S extends BaseVO> T voToBvo (S vo, Class<? extends BaseBVO> bvoClass) {
if (vo != null)
{
return (T) mapper.map(vo , bvoClass);
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我在服务模块中有一个使用该方法的方法:
"voToBvo (S vo, Class<? extends BaseBVO> bvoClass")
Run Code Online (Sandbox Code Playgroud)
像这样:
public List<AdcOptionBVO> …Run Code Online (Sandbox Code Playgroud) 为什么要mockMap使用真正的实现?我该如何防止这种情况?
在方法testFirstKeyMatch中
when(mockMap.keySet().toArray()[0])...
Run Code Online (Sandbox Code Playgroud)
抛出ArrayIndexOutOfBoundsException:运行测试时为0.
MaxSizeHashMap是一个最大大小为7的LinkedHashMap,当我尝试添加更多内容时抛出一个IndexOutOfBoundsException.
配置文件记录对此不重要的内容.
SuperClass.java
public class SuperClass {
protected String[] days;
protected MaxSizeHashMap<String, String> map;
public SuperClass() {
days = new String[7];
map = new MaxSizeHashMap<String, String>();
//...
}
void updateDays() {
cal = Calendar.getInstance();
for (int i = 0; i < 7; i = i + 1) {
//adds short names "Mon", "Tue", ... to days
days[i] = cal.getDisplayName(Calendar.DAY_OF_WEEK,
Calendar.SHORT, Locale.US);
cal.add(Calendar.DATE, 1);
}
}
void firstKeyMatch(Profile profile) {
updateDays();
//checks if …Run Code Online (Sandbox Code Playgroud) 我一直在尝试测试 CrudRepository 中的 findById() 方法。这个方法返回一个Optional,我不知道如何返回它,现在它给了我一个NullPointerException。
我的测试代码如下所示:
@RunWith(MockitoJUnitRunner.class)
public class DishServiceMockTest {
private static final String DISH_NAME = "Kaas";
private static final String DISH_TYPE = "Voorgerecht";
private static final Long DISH_ID = 23L;
//Mock the service dependencies(=DishServiceImpl is dependent on dishRepo)
@Mock
DishRepository dishRepository;
//Mock the service which is to be tested (Can't be a interface)
@InjectMocks
DishServiceImpl dishService;
@Test
public void findById(){
//Arange
Dish dish = createDish(DISH_ID, DISH_NAME, DISH_TYPE);
Mockito.when(dishRepository.findById(DISH_ID)).thenReturn(Optional.of(dish));
assertThat(dishService.findById(DISH_ID)).isEqualTo(dish);
}
Run Code Online (Sandbox Code Playgroud)
运行测试给出了 2 个错误,其中之一是 NullPointerException,第二个是: …
在此先感谢您的帮助.我想遍历工作簿中的所有工作表.不幸的是,我不知道给定工作簿中有多少工作表.现在我使用以下技术来枚举所有工作表:
Excel.Worksheet xlWorkSheet1;
xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Worksheet xlWorkSheet2;
xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
Excel.Worksheet xlWorkSheet3;
xlWorkSheet3 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
Run Code Online (Sandbox Code Playgroud)
是否有一种方法可以返回工作簿中的工作表数量?
我有点困惑 - 在 DB 中存储二进制数据的优点是什么?是出于安全原因,还是有一些我看不到的更复杂的动机?
谢谢你的时间。
我有一个像这样的目录结构和build.xml
/path-to-project/src/prj1
/path-to-project/src/prj2
/path-to-project/tests
/path-to-project/tests/build.xml
Run Code Online (Sandbox Code Playgroud)
我必须以某种方式得到路径
/path-to-project/
Run Code Online (Sandbox Code Playgroud)
在我的build.xml中
我的build.xml是这样的
<project name="php-project-1" default="build" basedir=".">
<property name="source" value="src"/>
<target name="phploc" description="Generate phploc.csv">
<exec executable="phploc">
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
<arg path="${source}" />
</exec>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
在这里,我莫名其妙地想要得到的值${source}作为/path-to-project/src/,但我没有用得到它${parent.dir}
是否可以在build.xml中获取此路径?
我有一个HashMap<String, String>我需要按特定顺序排序的。我了解 HashMaps,由于它们的构造方式,无法轻松排序。但是,我正在寻找重新排列 HashMap 中键的最佳策略 - 例如,我当前的 HashMap 如下所示:
"folioProperties": {
"strategy": "Strategy",
"summary": "Summary",
"initiative": "Initiative",
"deliveryMilestone": "Delivery Milestone",
"onTarget": "On Target",
"size": "Size",
"driver": "Driver",
"issueName": "Issue Name",
"releaseWindow": "Release Window",
"type": "Type",
"targetQuarter": "Target Quarter"
}
Run Code Online (Sandbox Code Playgroud)
我有一个表视图,将这些属性中的每一个显示为列,我需要按Strategy, Initiative, Issue Name, Summary, Delivery Milestone, On Target, Size, Target Quarter, Driver, Type, Release Window.
任何建议,将不胜感激!
PS 在构造 HashMap 之前,键在List<String>.
interface A{
void some();
}
@Component
class B implements A{
@override
some(){
}
}
@Component
class C implements A{
@override
some(){
}
}
Class D {
@Autowired
List<A> somes;//will it have the instances of both
}
Run Code Online (Sandbox Code Playgroud)
我正在开发一个项目,我们有多个类实现相同的接口.如何让D类中的列表包含B类和C类的bean?