小编dav*_*xxx的帖子

java中的设计模式无法识别

我有一个问题,下面的代码的设计模式的名称是什么.

FileInputStream fin = new FileInputStream("X.zip");  
BufferedInputStream bin = new BufferedInputStream(fin);
ZipInputStream zin = new ZipInputStream(bin);
Run Code Online (Sandbox Code Playgroud)

谁能帮我 ?谢谢.

java design-patterns inputstream

2
推荐指数
1
解决办法
126
查看次数

创建名为'dataSource'+ Spring Boot + Hibernate的bean时出错

关于如何访问mysql数据,我在启动spring指南时遇到了一些问题(请参阅此链接:https://spring.io/guides/gs/accessing-data-mysql/).我稍微调整了类,所以我有这个代码:

  1. 的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0      http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.springframework</groupId>
        <artifactId>gs-mysql-data</artifactId>
        <version>0.1.0</version>
    
        <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    </parent>
    
    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <!-- Use MySQL Connector-J -->
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    </dependencies>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    </build>
    
    Run Code Online (Sandbox Code Playgroud)

我正在使用这个mainController: MainController.java

import org.springframework.beans.factory.annotation.Autowired;
import …
Run Code Online (Sandbox Code Playgroud)

java mysql spring maven

2
推荐指数
1
解决办法
3万
查看次数

是否有兴趣在单元测试中测试基本增量?

是否有兴趣通过单元测试来测试基本增量?

测试类中的方法

@Test
public void testIncreaseString() throws Exception {

}
Run Code Online (Sandbox Code Playgroud)

原始课程中的方法

public void increaseString() {
    counterString++;
}
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing

1
推荐指数
1
解决办法
107
查看次数

声明类的字段而不在类前面添加前缀

private static final Color DEFAULT_PEN_COLOR   = BLACK;

private static final Color DEFAULT_CLEAR_COLOR = WHITE;
Run Code Online (Sandbox Code Playgroud)

这是我的代码的一部分,即使我输入import java.awt.Color时,有一个错误表明无法识别符号BLACK和WHITE.我需要做什么?

java

1
推荐指数
1
解决办法
53
查看次数

java中的通用链接列表

我正在学习泛型,并希望创建一个通用的链表.

但我得到了编译时错误.

Type mismatch: cannot convert from LinkedList<E>.Node<E> to
 LinkedList<E>.Node<E>
Run Code Online (Sandbox Code Playgroud)
public class LinkedList<E> {
    private Node<E> head = null;

    private class Node<E> {
        E value;
        Node<E> next;

        // Node constructor links the node as a new head
        Node(E value) {
            this.value = value;
            this.next = head;//Getting error here
            head = this;//Getting error here
        }
    }

    public void add(E e) {
        new Node<E>(e);
    }

    public void dump() {
        for (Node<E> n = head; n != null; n = n.next)
            System.out.print(n.value + …
Run Code Online (Sandbox Code Playgroud)

java generics linked-list

1
推荐指数
1
解决办法
1963
查看次数

如何在JUnit中打印错误的结果?

我正在阅读这个 JUnit教程,其中报告了这个例子:

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MyTests {

    @Test
    public void multiplicationOfZeroIntegersShouldReturnZero() {
        MyClass tester = new MyClass(); // MyClass is tested

        // assert statements
        assertEquals("10 x 0 must be 0", 0, tester.multiply(10, 0));
        assertEquals("0 x 10 must be 0", 0, tester.multiply(0, 10));
        assertEquals("0 x 0 must be 0", 0, tester.multiply(0, 0));
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是:如果测试失败,我如何打印(错误的)返回结果?就像是:

        assertEquals("0 x 0 must be 0, instead got"+tester.multiply(0, 0), 0, tester.multiply(0, 0));
Run Code Online (Sandbox Code Playgroud)

java testing junit unit-testing junit5

1
推荐指数
1
解决办法
446
查看次数

如何从目录中获取特定数量的文件?

我想根据我在properties文件中提供的设置检索文件.例如,我只想在第一次迭代中获得50个文件并停止获取所有文件夹中可能存在数千个文件.

我怎么能随机获得50个文件并且没有获得所有列表或迭代文件以获得50?

filesList = folder.listFiles( new FileFilter() {                
    @Override
    public boolean accept(File name) {                      
        return (name.isFile() && ( name.getName().contains("key1")));
    }
});
Run Code Online (Sandbox Code Playgroud)

编辑:我删除了for声明.即使我只提供了一个文件夹来获取它将获取所有文件,计数器变量仍然循环文件夹中的所有文件不是一个好的解决方案.

java file filefilter java-7

1
推荐指数
1
解决办法
164
查看次数

@ Before / @ BeforeEach继承行为更改JUnit4 / JUnit5

从JUnit4迁移到JUnit5时,我发现JUnit4和JUnit5的行为发生了变化,并想检查该变化是否是JUnit4或JUnit5中的错误,以及如何正确执行。

让我们假设以下结构:

一个基层

public class BaseTestClass {

    @Before
    public void setUp(){
        System.out.println("Base Test Class");
    }
}
Run Code Online (Sandbox Code Playgroud)

从该基类继承的另一个类

public class InheritsFromBase extends BaseTestClass {

    @Override
    public void setUp() {
        System.out.println("I inherit from base");

        super.setUp();
    }
}
Run Code Online (Sandbox Code Playgroud)

和一个实际的测试班

public class ActualTests extends InheritsFromBase {

    @Test
    public void myTest(){
        Assert.assertTrue(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我运行myTest()JUnit 4中,则setUp()两者的方法,BaseTestClassInheritsFromBase被调用。

将这段代码迁移到JUnit5之后,setUp()不再调用这些方法。我必须在上手动添加@BeforeEach注释InheritsFromBase

产生以下类别:

public class BaseTestClass {

    @BeforeEach
    public void setUp(){
        System.out.println("Base Test Class");
    }
} …
Run Code Online (Sandbox Code Playgroud)

java junit4 junit5

1
推荐指数
1
解决办法
1591
查看次数

@SpringBootTest测试类是否正确?

这是单元测试的正确本质吗?我想我不明白应该测试什么.ConverterContext是一个策略类

@SpringBootTest
@ExtendWith(SpringExtension.class)
class ConverterContextTest {

    @Autowired
    private final ConverterContext converterContext;
    @Autowired
    private final ConverterRegisterUserDto created;

    @Autowired
    ConverterContextTest(ConverterContext converterContext, ConverterRegisterUserDto created) {
        this.converterContext = converterContext;
        this.created = created;
    }

    @Test
    void converterContextGivesCorrectConverter(){
        ConverterRegisterUserDto returned = converterContext.getConverter(ConverterRegisterUserDto.class);
        assertEquals(returned, created);
    }

    @Test
    void converterContextGivesIncorrectConverter(){
        ConverterShowUserDto returned = converterContext.getConverter(ConverterShowUserDto.class);
        assertNotEquals(returned, created);
    }
}
Run Code Online (Sandbox Code Playgroud)

java junit spring unit-testing spring-boot

1
推荐指数
1
解决办法
322
查看次数

Spring Boot(2.0.4.RELEASE)-IllegalArgumentException:不是托管类型

我正在使用Spring Boot应用程序尝试与手动数据源连接,当运行项目时出现异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountController': Unsatisfied dependency expressed through field 'accountService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountServiceImpl': Unsatisfied dependency expressed through field 'accountDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.springstudy.demo.model.Account
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] …
Run Code Online (Sandbox Code Playgroud)

java hibernate spring-data-jpa spring-boot

1
推荐指数
2
解决办法
4385
查看次数