小编naz*_*art的帖子

如何在Java中测试写入文件?

我是初学者,掌握在手中.我有一些简单的程序,我需要对write方法进行junit测试.我在输入中有一些收藏.我怎么能这样做?这是我的代码:

// write to file
public void write(String fileName, 
        List<FigureGeneral> figuresList) {
    try {
        PrintWriter out = new PrintWriter(
                new File(fileName).getAbsoluteFile());
        try {
            for (int i = 0; i < figuresList.size(); i++) {
                out.println(figuresList.get(i).toString());
            }
        } finally {
            out.close();
        }
    } catch (IOException e) {
        System.out.println("Cannot write to file!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道,因为我从文件中读取后,我们可以加入两个测试(写/读)或者更好地单独执行(如果我们的测试失败,我们不知道哪里有问题 - 读或写)?如何在junit上正确地做这个(准备测试,并测试自己)?更好地展示示例,这种方式更好地理解.

谢谢你,纳扎尔.

java file-io junit unit-testing

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

确定数组列表是否已排序

我需要估计数组列表是否已排序(不排序).

字符串排序时,它们按字母顺序排列.我尝试使用compareTo()方法来确定首先出现的字符串

如果数组列表已排序,则返回true,否则返回false.

码:

public boolean isSorted()
{
    boolean sorted = true;        
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i-1).compareTo(list.get(i)) != 1) sorted = false;
    }

    return sorted;
}
Run Code Online (Sandbox Code Playgroud)

简单测试:

    ArrayList<String> animals = new ArrayList<String>();
    ArrayListMethods zoo = new ArrayListMethods(animals); 
    animals.add("ape");
    animals.add("dog");
    animals.add("zebra");

    //test isSorted
    System.out.println(zoo.isSorted());
    System.out.println("Expected: true");

    animals.add("cat");
    System.out.println(zoo.isSorted());
    System.out.println("Expected: false");

    animals.remove("cat");
    animals.add(0,"cat");
    System.out.println(zoo.isSorted());
    System.out.println("Expected: false");

    **Output:**
    false
    Expected: true
    false
    Expected: false
    false
    Expected: false
Run Code Online (Sandbox Code Playgroud)

这个简单的测试只显示了1/3覆盖率

如何解决这个问题.

java sorting output

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

在emacs上运行sml控制台-搜索程序:没有这样的文件或目录?

我在Ubuntu 12.04上安装了SML。

这是更多信息:

nazar_art@nazar-desctop:~$ sml -version
Standard ML of New Jersey v110.75 [built: Mon Sep  2 23:36:16 2013]
Run Code Online (Sandbox Code Playgroud)

并将此软件包添加到emacs:

M x list-packages  + Enter
Run Code Online (Sandbox Code Playgroud)

sml-mode单击鼠标查找并安装。但是,当我尝试编写简单.sml文件时,我想在emacs上打开sml控制台。

我按C c + C s=>我看到SML command: sml并按Enter

并看到此消息:

Searching for program: no such file or directory
Run Code Online (Sandbox Code Playgroud)

而不是打开sml控制台。

我不知道为什么会这样?

  • 如何解决这个麻烦?

更新:

这是我的which sml

/usr/local/SML/bin/sml
Run Code Online (Sandbox Code Playgroud)

和emacs版本- GNU Emacs 24.3.1此处安装,但仅适用于emacs24,不适用于emacs-snapshot。

emacs installation sml

5
推荐指数
2
解决办法
5924
查看次数

无法在Windows上从控制台创建maven项目 - 错误

我试图在Windows XP中生成maven项目.

我创建了文件夹pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>selenium.web.driver</groupId>
    <artifactId>OMS_selenium_Test</artifactId>
    <version>1.0</version>

    <packaging>jar</packaging>
    <name>selenium_tests</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.0</version>
            <scope></scope>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

打开cmd并进入此文件夹.

运行下一个命令:

C:\Documents and Settings\Admintemp\maven_test>mvn archetype:generate -DgroupId=
selenium.web.driver -DartifactId=OMS_selenium_Test -Dversion=1.0-SNAPSHOT -Dinte
ractiveMode=false
Run Code Online (Sandbox Code Playgroud)

我没有使用这些数据生成项目,而是抓住了下一个错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2
.2:generate (default-cli) on project OMS_selenium_Test: Unable to add module to
the current project as it is not of packaging type 'pom' -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with …
Run Code Online (Sandbox Code Playgroud)

java runtime-error maven

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

org.hibernate.exception.SQLGrammarException:无法执行语句

我在Hibernate上尝试了简单的程序并且遇到了一堆异常.

我无法弄清楚到底出了什么问题.

我有三个班 - 书,读者和使用.最后一个是绑定前两个,依赖一对多.

这是我的main():

public class Appl {
    public static void main(String[] args) {
        Book book = new Book();
        book.setTitle("book01155");
        //
        Reader reader = new Reader();
        reader.setName("reader2");
        //
        Using using = new Using();
        using.setIdBook(book);
        using.setIdReader(reader);
        //
        List<Book> elements = new ArrayList<Book>();
        //
        Session session = null;     
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(book);
            session.save(reader);
            session.save(using);
            elements = session.createCriteria(Book.class).list();
            session.getTransaction().commit();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        for (Book b : …
Run Code Online (Sandbox Code Playgroud)

java hibernate exception

5
推荐指数
1
解决办法
6万
查看次数

Java - 我们可以在构造函数中声明对象变量吗?

创建对象时,我们可以声明并初始化对象变量吗 constructor

我正在创建一个基于 XML 的对象。XML格式是以前系统的输出,是动态的。

因此对象变量是基于元素的。如果我在构造函数中声明变量会更好。

在下面的代码中我们可以将 temp 声明为对象变量。

例如:

public class A
{
    public A()
    {
        HashMap<String, Object> temp = new HashMap<String, Object>;
    }
}
Run Code Online (Sandbox Code Playgroud)

我找不到这种意识形态的任何示例代码。
有什么建议么?

java constructor

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

如何从TestNG测试和Selenium接口调用默认方法?

我想知道是否可以使用来自TestNG @BeforeMethod注释的接口的默认方法?

这是我试过的样本:

@Listeners(TestListener.class)
public interface ITestBase {
    String baseUrl = Config.getProperty(Config.TEST_HOST);
    String driverName = Config.getProperty(Config.BROWSER);
    DriversEnum driverInstance = DriversEnum.valueOf(driverName.toUpperCase());

    @BeforeMethod(alwaysRun = true)
    default public void start() {
        try {
            driver.init();
            DriverUnit.preconfigureDriver(Driver.driver.get());
            driver.get().manage().deleteAllCookies();
            driver.get().get(baseUrl);
        } catch (TimeoutException e) {
            Logger.logEnvironment("QT application is not available");
        }
    }

    @AfterMethod(alwaysRun = true)
    default public void end() {
        if (driver.get() != null) {
            try {
                driver.get().quit();
            } catch (UnreachableBrowserException e) {
                Logger.logDebug("UnreachableBrowser on close");
            } finally {
                driver.remove();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我运行典型的TestNG测试方法时,如:

public …
Run Code Online (Sandbox Code Playgroud)

java testng java-8 selenium-webdriver default-method

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

无法使用Spring Cloud连接到Hystrix仪表板的Command Metric Stream

我有Spring Cloud的微服务项目,这是父母的摘录:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

所有服务都在Eureka服务器下运行:

在此处输入图片说明

所有服务运行正常。我可以打给Postman适当的电话,一切正常。

我有单独的服务来处理Hystrix仪表板,摘录自pom

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

配置主类:

@SpringBootApplication
@EnableHystrixDashboard
public class DashboardApp {
    public static void main(String[] args) {
        SpringApplication.run(DashboardApp.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

和配置yaml文件:

spring:
  application:
    name: Dashboard

server:
  port: 8000

eureka:
  client:
    fetchRegistry: true
    registerWithEureka: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
Run Code Online (Sandbox Code Playgroud)

我有下一个仪表板:

在此处输入图片说明

来自控制台的完整堆栈跟踪在这里。以下是一些代码段: …

spring runtimeexception spring-boot hystrix spring-cloud

5
推荐指数
4
解决办法
6153
查看次数

如何在不对数组进行排序的情况下计算数组中的整数组?

我的目标是能够计算数组中相同整数的组。例如,在这样的数组中{1, 1, 1, 2, 2, 3, 1, 1},有4个组

  • 至少1:3组
  • 至少2:1组
  • 至少为3号

我在不对数组进行排序的情况下遇到了问题。当它被排序时,我在数组末尾失去了两个1的组的计数,因为它被放置在其他1的组旁边。

int result = (int) Stream.of(1, 1, 1, 2, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 4, 4, 4, 6)
        .collect(Collectors.groupingBy(i -> i))
        .entrySet().stream()
        .filter(entry -> entry.getValue().size() >= 1) // specify the size
        .count()

return result;
Run Code Online (Sandbox Code Playgroud)

每种尺寸的预期输出如下:

size 1 count == 8

size 2 count == 5

size 6 count == 1

size 8 count == …
Run Code Online (Sandbox Code Playgroud)

java arrays counting java-stream

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

简单的Java 2d阵列迷宫样本

我正在工作或了解如何创建一个简单的java 2d迷宫,它应该如下所示:

int [][] maze = 
{ {1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,0,1,0,1,0,1,0,0,0,0,0,1},
  {1,0,1,0,0,0,1,0,1,1,1,0,1},
  {1,0,0,0,1,1,1,0,0,0,0,0,1},
  {1,0,1,0,0,0,0,0,1,1,1,0,1},
  {1,0,1,0,1,1,1,0,1,0,0,0,1},
  {1,0,1,0,1,0,0,0,1,1,1,0,1},
  {1,0,1,0,1,1,1,0,1,0,1,0,1},
  {1,0,0,0,0,0,0,0,0,0,1,0,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1}

};
Run Code Online (Sandbox Code Playgroud)

这个已被创建的想法是设置一个起点和目标点,并通过使用递归深度首先找到路径.但必须说我有困难创造迷宫.

你有什么建议怎么做吗?
或者也许是教程的链接?
我现在主要关注的是创造迷宫.

java maze multidimensional-array depth-first-search

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