小编bra*_*orm的帖子

编写pytest函数来检查输出到python中的文件?

我问了这个问题,关于如何写一个pytest来检查输出stdout并得到一个解决方案.现在我需要写一个test case,检查内容是否写入文件,内容是否按预期写入,例如:

def writetoafile():
   file = open("output.txt",w)
   file.write("hello\n")
   file.write("world\n")
   file.close()
Run Code Online (Sandbox Code Playgroud)

现在是一个pytest函数来检查它是否写入:

def test_writeToFile():
    file = open("ouput.txt",'r')
    expected = "hello\nworld\n"
    assert expected==file.read()
Run Code Online (Sandbox Code Playgroud)

虽然这似乎有效,但我认为这不是理想的,尤其是硬编码.这些test functions写入文件通常是如何编写的?

python file-io function pytest

15
推荐指数
2
解决办法
9070
查看次数

Maven不同单元测试如何从集成测试?

这是我的项目结构mvn:

在此输入图像描述

你可以注意到,我有两节课 src/test/java

  1. CardValidtorIT.java (这是集成测试)

  2. CardValidatorTest.java (这是单元测试)

我跑的时候

mvn package

我注意到只有unit-test (CardValidatorTest.java)运行

但是当我跑步的时候

mvn integration-test

我看到了两个unit-test并且Integration tests正在运行.

怎么mvn知道CardValidatorIT.java在我跑的时候不执行mvn package.也就是说,为什么它没有运行CardValidatoryIT.java

这是我的 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>
  <parent>
    <artifactId>chapter14</artifactId>
    <groupId>org.agoncal.book.javaee7</groupId>
    <version>1.0</version>
</parent>
<groupId>org.agoncal.book.javaee7.chapter14</groupId> <artifactId>chapter14-service</artifactId> <version>1.0</version>
<packaging>war</packaging>
  <dependencies>
    <dependency>
<groupId>org.glassfish.main.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>4.0</version>
<scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin> …
Run Code Online (Sandbox Code Playgroud)

integration-testing unit-testing maven

14
推荐指数
2
解决办法
7244
查看次数

如何避免cassandra中的二级索引?

我一再听说二级索引(在cassandra中)只是为了方便而不是为了获得更好的性能.建议在基数较低时使用二级索引的唯一情况(例如性别column为男性或女性的两个值)

考虑这个例子:

CREATE TABLE users ( 
userID uuid, 
firstname text, 
lastname text, 
state text, 
zip int, 
PRIMARY KEY (userID) 
);
Run Code Online (Sandbox Code Playgroud)

现在,除非我在users on上创建二级索引,否则我无法执行此查询firstname index

select * from users where firstname='john'
Run Code Online (Sandbox Code Playgroud)

如何对此表进行非规范化,以便我可以使用此查询:这是使用复合键的唯一有效方法吗?还有其他选择或建议吗?

CREATE TABLE users ( 
    userID uuid, 
    firstname text, 
    lastname text, 
    state text, 
    zip int, 
    PRIMARY KEY (firstname,userID) 
    );
Run Code Online (Sandbox Code Playgroud)

cql3 secondary-indexes cassandra-2.0

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

如果我使用Spring Boot开发Web应用程序作为战争,为什么我需要main方法?

我正在使用Spring Boot开发Web应用程序.我的典型部署是生成war并将其webapps放在Tomcat目录中的文件夹中.

我注意到SpringBoot,我需要一个main方法.我想知道为什么这是必要的.如果有办法避免它,那会是什么?

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-boot

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

如何在Jframe Swing中居JLabel?

我有这个用于Swing秒表的工作代码.我希望标签Time Remaining 300 seconds以第二行为中心.这是我的代码.在此输入图像描述

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TimerGUI extends JPanel {
    JLabel promptLabel, timerLabel;
    final int count = 30;
    JButton start;
    JButton end;
    Timer timer;

    public TimerGUI() {
        setLayout(new GridLayout(0,2));

        start = new JButton("Start");
        add(start);
        Event e = new Event();
        start.addActionListener(e);

        end = new JButton("End");
        end.setEnabled(false);
        add(end);
        end.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                start.setEnabled(true);
                end.setEnabled(false); …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel centering

12
推荐指数
3
解决办法
6万
查看次数

给定一个图表来检测它是否是定向和无向图形中的树?

我想知道一个快速算法,以确定给定的图形是否是一棵树. 这篇文章似乎处理它,但不是很清楚.根据这个,如果图是非循环的,那么它就是一棵树.如果你考虑所示的有向和无向图的例子,在我看来只有1和4是树,但我认为3既不是循环也不是树.在此输入图像描述

所以我的问题是:需要检查什么才能有效地检查图形是否为树,无论是有向图还是无向图?

领先一步看看,如果树存在,那么它是否是二叉树?

algorithm tree graph cyclic

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

在Java中实现ArrayList时键入擦除

我正在阅读关于Java Generics的这篇文章,并且在那里提到了一个ArrayList看起来像这样的构造函数:

class ArrayList<V> {
  private V[] backingArray;
  public ArrayList() {
    backingArray = (V[]) new Object[DEFAULT_SIZE]; 
  }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解编译器的类型擦除和类型检查是如何解释的那样.我得到的一点是type参数转换为Objecttype.

我将它想象成(更换所有VObject),但是这肯定是不对的.

class ArrayList<Object> {
      private Object[] backingArray;
      public ArrayList() {
        backingArray = (Object[]) new Object[DEFAULT_SIZE]; 
      }
}
Run Code Online (Sandbox Code Playgroud)

它究竟是如何转变为Object类型但仍保留类型安全性的V?当我有ArrayList<String>ArrayList<Integer>是否有两种不同类型的每一个?如果没有,在哪里类型的信息StringInteger存储?

java generics constructor arraylist object

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

415 spring应用程序中的POST请求不支持的MediaType

我有一个非常简单的Spring应用程序(不是弹簧启动).我已经实现了GET和POST控制器方法.该GET方法工作正常.但是在POST415 Unsupported MediaType.下面提供了重现步骤

ServiceController. java

package com.example.myApp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


    @Controller
    @RequestMapping("/service/example")
    public class ServiceController {

        @RequestMapping(value="sample", method = RequestMethod.GET)
        @ResponseBody
    public String getResp() {
        return "DONE";
    }

    @RequestMapping(value="sample2", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public String getResponse2(@RequestBody Person person) {
        return "id is " + person.getId();
    }

}

class Person {

    private int id;
    private String name;

    public Person(){

    }

    public int getId() …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-restcontroller

12
推荐指数
2
解决办法
4万
查看次数

为什么继承是强耦合的,因为组合在Java中松散耦合?

favor composition over inheritance在设计模式中一次又一次地听到这个.引用的一些原因是

1)Inheritance is strongly coupled where as composition is loosely coupled
2) Inheritance is compile time determined where as composition is run-time
3)Inheritance breaks encapsulation where as composition does not
4) anything else I am not aware of
Run Code Online (Sandbox Code Playgroud)

对于像我这样的初学者来说,通过插图来理解遗传和构图在上述方面的不同之处将会很棒.我已经阅读了各种谈论它们的SO链接,但是通过这些关键点的示例对Java初学者来说非常有用.

我认为清楚地理解差异非常重要,而不仅仅是记住要点.

java inheritance design-patterns composition

11
推荐指数
2
解决办法
4651
查看次数

Google收到的每个请求都会打开多少个套接字?

以下是我最近与知名网络软件公司的面试经历.我被问到有关互连TCP级别和Web请求的问题,这让我很困惑.我真的很想知道答案的专家意见.它不仅涉及面试,还涉及对网络如何工作的基本理解(或者应用层和传输层如何交叉,如果他们这样做的话).

  1. 采访者:告诉我打开浏览器并输入内容后幕后发生的过程google.com.

    :首先发生的是创建一个套接字,由...标识{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL}.该 SRC-PORT数字是浏览器给出的随机数.通常是TCP/IP连接协议(建立三次握手).现在,客户端(我的浏览器)和服务器(Google)都准备好处理请求.(建立TCP连接).

  2. 采访者:等一下,名称解析何时发生?

    :是的,对不起.它应该在创建套接字之前发生.DNS名称解析首先发生以获取Google的IP地址.

  3. 访问者:是否为DNS名称解析创建了一个套接字?

    :嗯,我实际上不知道.但我所知道的DNS名称解析是无连接的.也就是说,它不是TCP而是UDP.只发生一个请求 - 响应周期.(因此,为DNS名称解析创建了一个新的套接字).

  4. 采访者:google.com对其他客户的其他请求开放.那么与谷歌建立联系阻止其他用户呢?

    :我不确定谷歌如何处理这个问题.但在典型的套接字通信中,它在最小程度上是阻塞的.

  5. 采访者:您认为如何处理?

    :我猜这个过程会分叉一个新线程并创建一个套接字来处理我的请求.从现在开始,我与Google通信的套接字端点就是这个子套接字.

  6. 采访者:如果是这种情况,这个子套接字的端口号是否与父套接字不同?

    :父套接字在80处侦听来自客户端的新请求.孩子必须在不同的端口号上听.

  7. 采访者:由于您的目标端口号已更改,您的TCP连接如何维护.(这是在Google的数据包上发送的src-port号码)?

    我: 我看到作为客户端的目标端口始终为80.当回复响应时,它也来自端口80.我猜操作系统/父进程将源端口设置回80,然后再发回帖子.

  8. 采访者:你的套接字连接与谷歌有多长时间了?

    :如果我在一段时间内没有提出任何请求,主线程会关闭它的子套接字,来自我的任何后续请求都将像我是一个新客户端.

    采访者:不,Google不会为您保留专用的子插座.它处理您的请求并立即丢弃/回收插座.

  9. 采访者:虽然谷歌可能有很多服务器来处理请求,但每个服务器只能在端口80打开一个父插槽.访问谷歌网页的客户端数量必须大于他们拥有的服务器数量.这通常如何处理?

    :我不确定如何处理.我看到它可以工作的唯一方法是为它收到的每个请求生成一个线程.

  10. 采访者:您认为Google处理此问题的方式与任何银行网站不同吗?

    :在TCP-IP套接字级别,它应该是类似的.在请求级别,略有不同,因为维护会话以保持银行网站请求之间的状态.

如果有人可以对每个要点给出解释,那么对于许多网络初学者来说这将是非常有帮助的.

sockets port networking tcp request

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