我在java 8中看到了一个迭代集合的代码.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
numbers.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
功能是System.out::println
什么?以上代码如何迭代List.
运营商的用途是::
什么,我们可以在哪里使用这个运营商?
以下两个代码段之间有什么区别.
public Integer getId(@Nonnull SomeObject obj){
// do some stuff
return id;
}
public Integer getId(SomeObject obj){
Objects.requireNonNull(SomeObject, "SomeObject is null");
// do some stuff
return id;
}
Run Code Online (Sandbox Code Playgroud)
它们之间有什么重大差异.在这些情况下进行空检查的正确方法是什么.
如果我有类似下面的课程,
import lombok.AllArgsConstructor;
@AllArgsConstructor
class MyClass{
private String one;
private Integer three;
private Integer two;
}
Run Code Online (Sandbox Code Playgroud)
生成的构造函数中的参数顺序是什么?它总是像下面一样,
public MyClass(String one, Integer three, Integer two) {
this.one = one;
this.three = three;
this.two = two;
}
Run Code Online (Sandbox Code Playgroud)
我注意到它是类本身的声明顺序.但需要确认一下.找不到任何验证该事实的文档.
如果不能,我们还能确定params的顺序吗?
我有一个像下面这样的2D数组.(array[5][2]
)
20 11
10 20
39 14
29 15
22 23
Run Code Online (Sandbox Code Playgroud)
排序后应该如下所示.
10 20
20 11
22 23
29 15
39 14
Run Code Online (Sandbox Code Playgroud)
这意味着应该对数组进行排序,仅比较第一列值.
在Java中,有一个内置的功能来实现这一点.如下.
Arrays.sort(a, new Comparator<Long[]>() {
@Override
public int compare(Long[] o1, Long[] o2) {
Long t1 = o1[1];
Long p1 = o1[0];
Long t2 = o2[1];
Long p2 = o2[0];
if (t1 == t2) {
return (p1 > p2 ? 1 : (p1 == p2 ? 0 : -1));
} else {
return (t1 < t2 …
Run Code Online (Sandbox Code Playgroud) 正如这里提到的,我们可以使用以下方法运行测试方法,
mvn -Dtest=TestCircle#xyz test
Run Code Online (Sandbox Code Playgroud)
但是我需要在运行测试用例之前设置一些 JVM 参数。就像我需要使用
-Djava.security.manager -Djava.security.policy=mypolicy.policy
我如何告诉 Maven 在运行测试用例时考虑这些。
我尝试重构代码,以便它将使用单独的方法来进行一些计算.只是为了说清楚.
我想知道的是,编写一个单独的方法来找出一个像数字奇数甚至是奇数的简单方法是一个好习惯还是坏习惯 ?
原始代码是,
int n = 11;
if (n % 2 == 0) {
System.out.println("Not selected");
} else {
boolean isPrime = true;
if (n == 0 || n == 1) {
isPrime = false;
} else {
int i = 2;
double a = Math.sqrt(Math.abs(n));
while (i <= a) {
if (n % i == 0) {
isPrime = false;
}
++i;
}
}
if(isPrime){
System.out.println("Prime it is");
}
}
Run Code Online (Sandbox Code Playgroud)
重构的代码是,
int n = 11;
if (isEven(n)) …
Run Code Online (Sandbox Code Playgroud) 我开发了Google Chrome扩展程序.我该怎么测试呢?如果只有普通的javaScript,它是直截了当的.但在我的方法中,有很多Chrome API参考可用.也不可能进行单元测试.
那么如何使用这些API功能测试我的扩展.?
integration-testing unit-testing google-chrome google-chrome-extension
我正在使用wiremock来模拟 Web服务.我正在使用它来提供对我们正在测试的某些单元的服务访问.所以基本上我们有多个junit测试,它们使用来自wiremock的存根.
但是当我们一次运行多个测试时(假设运行一个完整的test.java文件),它会在完成一些测试用例后继续等待.以下内容将显示在日志的底部,
[qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0]
DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop waiting on select
Run Code Online (Sandbox Code Playgroud)
Junit版本:4.12
Wiremock版本:2.5.0(wiremock-standalone)
Java版本:1.8.0_77
我还检查了这个问题.但仍然无法弄清楚如何克服这一点.
在我的申请中
`CategoryDao` is a `interface`, `Category` is a model `class`
Run Code Online (Sandbox Code Playgroud)
我的代码是
CategoryTestCase.java
package com.binod.onlineshopping.category.test;
import com.binod.onlineshopping.category.dao.CategoryDao;
import com.binod.onlineshopping.category.model.Category;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.assertEquals;
/**
* Created by binod on 7/13/17.
*/
public class CategoryTestCase {
private static AnnotationConfigApplicationContext context;
private static CategoryDao categoryDao;
private Category category;
@BeforeClass
public static void init() {
context = new AnnotationConfigApplicationContext();
context.refresh();
categoryDao = (CategoryDao) context.getBean("categoryDao");
}
@Test
public void addCategory(){
category=new Category();
category.setCname("Television");
category.setCdescription("TV is the product");
category.setImageUrl("c_Tv.png");
assertEquals("sucessfully inserted..",true,categoryDao.addCategory(category)); …
Run Code Online (Sandbox Code Playgroud) 我正在使用该assertEquals()
方法jUnit
来测试某个值是否与代码生成的实际值相等.
/*
calculating the actual_value
*/
int expected_value = 1000; // rows of the set of files, manually calculated
assertEquals(expected_value, actual_value);
Run Code Online (Sandbox Code Playgroud)
在标准和手续的情况下,我想知道如果我这样做,那将是一个问题.
/*
calculating the actual_value
*/
int expected_value = getRelevantLinesOfFiles(set of files); // rows of the set of files
assertEquals(expected_value, actual_value);
Run Code Online (Sandbox Code Playgroud)
因为几乎不可能总是手动找到那种变量,所以我写了一个方法来读取和计算这些文件中的相关行.
我担心的是我在assertEquals
测试中使用了一种方法.但该getRelevantLinesOfFiles()
方法未经过测试.如果我要测试它,那么我必须再次手动读取文件.所以它一次又一次有点相同.
这是一个好习惯吗?或者进行这类测试的最佳方法是什么?
java ×8
junit ×4
unit-testing ×3
arrays ×1
c++ ×1
java-8 ×1
lombok ×1
maven ×1
refactoring ×1
sorting ×1
spring ×1
spring-mvc ×1
testing ×1
wiremock ×1