junit

Junit @AfterClass (non static)

流过昼夜 提交于 2021-01-26 21:52:15
问题 Junit's @BeforeClass and @AfterClass must be declared static. There is a nice workaround here for @BeforeClass . I have a number of unit tests in my class and only want to initialize and clean up once. Any help on how to get a workaround for @AfterClass ? I'd like to use Junit without introducing additional dependencies. Thanks! 回答1: If you want something similar to the workaround mentioned for @BeforeClass , you could keep track of how many tests have been ran, then once all tests have been

LeetCode

痞子三分冷 提交于 2021-01-26 02:28:25
LeetCode - Easy - 326. Power of Three Topic Math Description https://leetcode.com/problems/power-of-three/ Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three, if there exists an integer x such that n == 3x. Example 1 : Input: n = 27 Output: true Example 2 : Input: n = 0 Output: false Example 3 : Input: n = 9 Output: true Example 4 : Input: n = 45 Output: false Constraints : -2³¹ <= n <= 2³¹ - 1 Analysis 略 Submission public class PowerOfThree { //方法一:switch版 public boolean isPowerOfThree1(int num) { switch (num) { case 1: return

Java单元测试指南

六眼飞鱼酱① 提交于 2021-01-25 07:46:33
单元测试是开发过程的关键环节。它们允许以可重复执行、可维护的方式对代码进行快速、简单的测试。具体来说,单元测试有以下优点: 在开发期间通过测试捕获缺陷。较小的工作单元更容易编码和调试。 对代码更改进行轻松的测试。因为单元测试是设计来测试业务用例的,如果技术实现发生了变化,那么单元测试可以重新运行以确保程序的结果没有发生变化。 验证在产品测试过程中可能没有被覆盖的罕见边角情况。 向同伴、主管和客户展示质量的程度。 单元测试的一个重要特点是方法必须得单独测试,这与主要测试方法与类之间交互的组装或集成测试不同,单元测试应该独立地测试方法。这有助于查明问题,并大大提高问题的修复速度。 单元测试应该尽可能频繁地运行。这将确保缺陷被立即发现并修复。自动化单元测试执行的一种方法是使用持续集成系统,该系统在每次源代码做出更改时运行所有单元测试。 该指南的对象将是: 技术架构师 数据架构师 应用程序设计师 开发人员 01 构建一个好的单元测试 为了确保类被适当地测试,所以在构建测试场景时必须小心: 通过一次只测试一个业务路径来保持单个测试的简单性。如果只有一条路径的测试失败,则更容易找到错误并纠正它。较短的测试也更容易阅读和维护,更容易作为新测试的构建块重用。 当测试“搜索”方法时,应该考虑多种数据情况。通常,应该创建单独的测试条件来测试返回零条、一条和多条记录以及使用零个

LeetCode

女生的网名这么多〃 提交于 2021-01-25 04:15:26
Topic Binary Search Math Description https://leetcode.com/problems/valid-perfect-square/ Given a positive integer num, write a function which returns True if num is a perfect square else False. Follow up : Do not use any built-in library function such as sqrt . Example 1 : Input: num = 16 Output: true Example 2 : Input: num = 14 Output: false Constraints : 1 <= num <= 2³¹ - 1 Analysis 方法一:将符合要求的数缓存好,再二分查找。 方法二:巧用奇数数列求和公式。 $$1 = 1$$ $$4 = 1 + 3$$ $$9 = 1 + 3 + 5$$ $$16 = 1 + 3 + 5 + 7$$ $$25 = 1 + 3 + 5 + 7 + 9$$ $$36 = 1 + 3 + 5 + 7 + 9 + 11$$ $$. . .$$ $$1+3+. . .+(2n-1)=\frac{(2n-1 + 1)\cdot

单元测试-java

空扰寡人 提交于 2021-01-24 02:50:10
单元测试-java Maven项目 ​ 学java必学!!! 如果你不愿意变强,请百度搜索idea或Eclipse怎么使用Junit。 1. junit 测试 ​ 单元测试,也需要遵循一定的编程规范,不能随便的写一个地方,也要做到 格式规范,命名规范,结构规范 等等。 例如我要测试Demo类的方法: 怎么使用? 编写测试类 编写测试方法 点击运行 例如: import junit.framework.TestCase; import org.junit.Test; public class DemoTest { @Test public void fun1() { System.out.println(new Demo().fun1(2,1,5)); double v = new Demo().fun1(2, 1, 6); System.out.println(v); // 断言 TestCase.assertEquals(v,5.0); } } 2. main方法测试 ​ 对于单个简单的方法测试,我们可以通过main方法直接测试 例如: public class Demo { public static double fun1(int a, int b, double c){ if(a>0 && b>0){ c = c/a; } if(a>1 || c>1){ c = c + 1

JUnit test description

末鹿安然 提交于 2021-01-21 12:05:34
问题 Is it possible in JUnit to add a brief description of the test for the future reader (e.g. what's being tested, some short explanation, expected result, ...)? I mean something like in ScalaTest, where I can write: test("Testing if true holds") { assert(true) } Ideal approach would be using some annotation, e.g. @Test @TestDescription("Testing if true holds") public void testTrue() { assert(true); } Therefore, if I run such annotated tests using Maven (or some similar tool), I could have

Spring之IOC容器

眉间皱痕 提交于 2021-01-21 04:16:57
在前面博客中介绍什么是依赖注入时有提到:依赖注入是组件之间依赖关系由容器在运行期决定,即由容器动态的将某个依赖关系注入到组件之中。那什么是容器?既然Spring框架实现了IOC,那Spring中的容器是什么呢? 一、容器介绍 在日常生活中容器是指用以容纳物料并以壳体为主的基本装置,它是用来盛放东西的。在编程中容器是用来存储和组织其他对象的对象,首先要确定容器也是对象,也可以当做bean,只是这个对象是用来存储和组织其他对象,那其他对象是什么呢?其他对象其实就是bean对象,这也是面向对象编程的一种体现,万物皆对象。在Spring提供了BeanFactory、ApplicationContext两个IOC容器,来管理众多的bean对象。 二、BeanFactory 一提到工厂我们生活当中可能会想到富某康,工厂是一类用以生产货物的大型工业建筑物。而BeanFactory不是用来生产货物的而是用来生产管理bean的。BeanFactory会在bean的生命周期的各个阶段中对bean进行各种管理,并且Spring将这些阶段通过各种接口暴露给我们,让我们可以对bean进行各种处理,我们只要让bean实现对应的接口,那么Spring就会在bean的生命周期调用我们实现的接口来处理该bean。那它是怎么实现的呢?它主要分两个阶段。 1)、Bean容器的启动

Spring Spring boot 获取IOC中的bean,ApplicationContext

China☆狼群 提交于 2021-01-21 03:24:01
https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d9 https://blog.csdn.net/zsw12013/article/details/51701671 ____________________________________________________________________________________________________________________________ SpringBoot中获取ApplicationContext的三种方式 ApplicationContext是什么? 简单来说就是Spring中的容器,可以用来获取容器中的各种bean组件,注册监听事件,加载资源文件等功能。 Application Context获取的几种方式 1 直接使用Autowired注入 @Component public class Book1 { @Autowired private ApplicationContext applicationContext; public void show (){ System.out.println(applicationContext.getClass(

实战 | 使用maven 轻松重构项目

蓝咒 提交于 2021-01-15 02:35:25
现在是微服务盛行时代,说不准哪一天领导就会让你对一个大项目进行重构。大项目的痛点:编译慢、发布繁琐等。就像下面这张图: 真的不敢动呀,一不小心就坍塌了。 比如说我们用户系统,我们可以这么重构(这里只是举例,每个项目拆分目的可能不同)。 user-system拆分成: user-web user-service user-dao user-common 我们对其拆分后很有可能存在多个子项目中同时依赖某个jar包,如果使用不当可能会导致每个模块使用的版本不一样,所以想想能不能有什么方式来解决这类问题呢? 答案:能。但是得先了解两个概念:聚合和继承。 聚合 所谓的聚合就是我们如果想一次性构建多个项目模块,那我们就需要把这些项目模块进行聚合。 配置模板 <modules> <module>项目模块一</module> <module>项目模块二</module> <module>项目模块三</module> <module>项目模块...</module> </modules> 使用方式 比如我们对user-web、user-service、user-dao、user-common四个项目模块进行聚合。 <modules> <module>user-web</module> <module>user-service</module> <module>user-dao</module>

Sping框架中的注解详解

风流意气都作罢 提交于 2021-01-14 03:01:20
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,这么做有两个缺点: 1、如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大;如果按需求分开.xml文件,那么.xml文件又会非常多。总之这将导致配置文件的可读性与可维护性变得很低。 2、在开发中在.java文件和.xml文件之间不断切换,是一件麻烦的事,同时这种思维上的不连贯也会降低开发的效率。 为了解决这两个问题,Spring引入了注解,通过"@XXX"的方式,让注解与Java Bean紧密结合,既大大减少了配置文件的体积,又增加了Java Bean的可读性与内聚性。 不使用注解: 先看一个不使用注解的Spring示例,在这个示例的基础上,改成注解版本的,这样也能看出使用与不使用注解之间的区别,先定义一个老虎: package com.spring.model; public class Tiger { private String tigerName="TigerKing" ; public String toString(){ return "TigerName:"+ tigerName; } } 再定义一个猴子: package com.spring.model; public class Monkey { private String monkeyName =