哪一个是更好的Java编码风格?
boolean status = true;
if (!status) {
//do sth
} else {
//do sth
}
Run Code Online (Sandbox Code Playgroud)
要么:
if (status == false) {
//do sth
} else {
//do sth
}
Run Code Online (Sandbox Code Playgroud) 用mock编写单元测试时遇到问题.有一个我需要模拟的对象有很多getter,我在代码中调用它们.但是,这些不是我单元测试的目的.那么,是否有一种方法可以模拟所有方法而不是逐个模拟它们.
这是代码示例:
public class ObjectNeedToMock{
private String field1;
...
private String field20;
private int theImportantInt;
public String getField1(){return this.field1;}
...
public String getField20(){return this.field20;}
public int getTheImportantInt(){return this.theImportantInt;}
}
Run Code Online (Sandbox Code Playgroud)
这是我需要测试的服务类
public class Service{
public void methodNeedToTest(ObjectNeedToMock objectNeedToMock){
String stringThatIdontCare1 = objectNeedToMock.getField1();
...
String stringThatIdontCare20 = objectNeedToMock.getField20();
// do something with the field1 to field20
int veryImportantInt = objectNeedToMock.getTheImportantInt();
// do something with the veryImportantInt
}
}
Run Code Online (Sandbox Code Playgroud)
在测试类中,测试方法就像
@Test
public void testMethodNeedToTest() throws Exception {
ObjectNeedToMock o = mock(ObjectNeedToMock.class);
when(o.getField1()).thenReturn(anyString());
.... …Run Code Online (Sandbox Code Playgroud) 我有一个春季启动项目希望通过springbox与swagger集成.
我的弹簧启动应用程序运行良好.
但是在我添加springbox之后,它无法通过单元测试.
以下是我在项目中添加的详细信息.
对于pom.xml,补充说
<!--Swagger io for API doc-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后使用swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket booksApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("blah")
.description("blah.")
.termsOfServiceUrl("http://www.blah.com.au")
.contact("blah")
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
运行时我得到的错误mvn clean package是
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/Users/jasonfeng/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency …Run Code Online (Sandbox Code Playgroud) 最近,我们将tomcat/spring应用程序转换为spring boot.除了新的遗物,一切都很好.有没有办法我可以轻松配置弹簧启动项目的新遗物.我不想硬编码新的relic agent jar路径的位置,然后用路径运行spring boot项目.
编辑:Spring启动项目是maven
我尝试以序列.nextval作为主键插入到表中,Java中的sql是
sql = "INSERT INTO USER
(USER_PK, ACCOUNTNUMBER, FIRSTNAME, LASTNAME, EMAIL )
VALUES
(?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "User.nextval");
ps.setString(2, accountNumber);
ps.setString(3, firstName);
ps.setString(4, lastName);
ps.setString(5, email);
Run Code Online (Sandbox Code Playgroud)
但是,错误是 ORA-01722: invalid number
所有其他领域都是正确的,我认为这是顺序问题,这是正确的吗?
我已经google了一段时间,不确定Spring Data MongoDB是否支持批量保存.
我需要将一组文档保存为mongo作为原子,要么全部保存,要么都不保存.
任何人都可以共享链接或一些示例代码吗?
我有问题在neo4j中检索路径排除某些标签.
敌人的例子,我有
-->(h)-->(j)
/
(a)-->(b)-->(c)-->(d)-->(i)
\
-->(f)-->(g)
Run Code Online (Sandbox Code Playgroud)
with hnode有一个Deleted标签.
我有疑问
MATCH path = (n)-[*]->(child) where id(n)={id of node a} and NOT child:Deleted RETURN path
Run Code Online (Sandbox Code Playgroud)
那么我想这个查询返回的完整路径,但不包括节点的子树h,因为节点h是Deleted.
返回树应该是这样的
(a)-->(b)-->(c)-->(d)-->(i)
\
-->(f)-->(g)
Run Code Online (Sandbox Code Playgroud)
但查询似乎无效.
谁能帮我这个.
谢谢
我有一个文件包含几行,每行的格式都是这样的
2011-07-10 condition hhh aaa: value bbb
2011-07-10 condition ccc aaa: value bbb
Run Code Online (Sandbox Code Playgroud)
我想用SED找到字符串值,这是与"AAA"和"BBB",并替换为"gotit"的基础上,继条件的字符串是CCC的条件.在sed之后,这个文件变成了
2011-07-10 condition hhh aaa: value bbb
2011-07-10 condition ccc aaa: gotit bbb
Run Code Online (Sandbox Code Playgroud)