junit

How to JUnit test a Method with a Scanner?

做~自己de王妃 提交于 2020-12-12 11:11:37
问题 I have a class which reads a file and takes in a user input with a scanner and if the scanner equals a part of a line in that file, it will display a string from the same line. How would I go and create a Junit test method for this? Here is some of my code that I want a test method for: Scanner Input = new Scanner(System.in); String name = Input.nextLine(); BufferedReader br; try{ br = new BufferedReader(new FileReader(new File(filename))); String nextLine; while ((nextLine = br.readLine()) !

How to JUnit test a Method with a Scanner?

↘锁芯ラ 提交于 2020-12-12 11:09:51
问题 I have a class which reads a file and takes in a user input with a scanner and if the scanner equals a part of a line in that file, it will display a string from the same line. How would I go and create a Junit test method for this? Here is some of my code that I want a test method for: Scanner Input = new Scanner(System.in); String name = Input.nextLine(); BufferedReader br; try{ br = new BufferedReader(new FileReader(new File(filename))); String nextLine; while ((nextLine = br.readLine()) !

JUnit asserting two Strings whether they're equal or not

和自甴很熟 提交于 2020-12-11 05:22:53
问题 So I looked up this question and tried it, but with no success. My code should be testing if the method is correctly outputing the text onto the console by reading it back in using Streams . ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream myStream = new PrintStream(outStream); System.setOut(myStream); o.doSomething(); //printing out Hi System.out.flush(); System.setOut(savedOldStream);//setting it back to System.out assertEquals(outStream.toString(),"Hi"); but

Write a unit test for downloading a file

风流意气都作罢 提交于 2020-12-05 05:21:06
问题 At the moment I wrote a small downloadService which let's user download a file (at the moment only excel). The code works properly, however I do not know how to write the unit test for it. That's my code: package com.pzm.service; import com.pzm.model.UserBillingsMock; import com.pzm.model.report.ExcelReport; import com.pzm.model.report.Report; import com.pzm.model.report.ReportFactory; import org.springframework.stereotype.Repository; import javax.servlet.ServletOutputStream; import javax

JDBC实现增删改查

﹥>﹥吖頭↗ 提交于 2020-12-05 05:04:18
添加 mysql,c3p0,junit 依赖 : 连接数据库 : 1. 直接连接数据库 注意 : 1. 不需要使用 Class.forName() 哦 , 原因请查看源码 2. useSSL=false 只是为了屏蔽掉 Mysql 的警告信息 , 可 以不加 2. 使用连接 池 在 resources 目录下添加 c3p0-config.xml 配置文件 说明 : 这里仅仅是测试 , 所以我放在了 test/ resources/ 目录下 , 非测试请放 在 main/resources/ 下 . 创建用户信 息 : 上面提供了返回主键和不返回主键两种写法 查询用户信息 : 推荐第二种方式 , 不会因为表字段位置调整修改代码 , 并且通过泛型和反射可实现查询任意表和返回给定对象 . 更新用户信息 : 删除用户信息 : 后面将会介绍 : emmmmmm... 还没想好 github : https://github.com/RamerF/spring-web.git PC 阅读文章 : ramer.iask.in 有任何疑问 , 请联系 微信 : ramer- QQ : 1874890499 本文分享自微信公众号 - JavaWeb开发技术(spring-web)。 如有侵权,请联系 support@oschina.cn 删除。 本文参与“ OSC源创计划 ”,欢迎正在阅读的你也加入

“Cannot resolve method” with mockito

痞子三分冷 提交于 2020-12-04 15:57:17
问题 I use org.springframework.security.core.Authentication which has a method: Collection<? extends GrantedAuthority> getAuthorities(); I want to mock it as below: when(authentication.getAuthorities()).thenReturn(grantedAuthorities); with authorities collection: Collection<SimpleGrantedAuthority> grantedAuthorities = Lists.newArrayList( new SimpleGrantedAuthority(AuthoritiesConstants.USER)); And I am using org.springframework.security.core.authority.SimpleGrantedAuthority which extends

“Cannot resolve method” with mockito

主宰稳场 提交于 2020-12-04 15:57:06
问题 I use org.springframework.security.core.Authentication which has a method: Collection<? extends GrantedAuthority> getAuthorities(); I want to mock it as below: when(authentication.getAuthorities()).thenReturn(grantedAuthorities); with authorities collection: Collection<SimpleGrantedAuthority> grantedAuthorities = Lists.newArrayList( new SimpleGrantedAuthority(AuthoritiesConstants.USER)); And I am using org.springframework.security.core.authority.SimpleGrantedAuthority which extends

jacoco + eclipse单元测试覆盖率

空扰寡人 提交于 2020-12-04 13:55:43
概念 Jacoco:JaCoCo是一个开源的覆盖率工具,它针对的开发语言是java,其使用方法很灵活,可以嵌入到Ant、Maven中;可以作为Eclipse插件,可以使用其JavaAgent技术监控Java程序等等。 插桩:程序插桩,它是在保证被测程序原有逻辑完整性的基础上在程序中插入一些探针(又称为“探测仪”,本质上就是进行信息采集的代码段,可以是赋值语句或采集覆盖信息的函数调用),通过探针的执行并抛出程序运行的特征数据,通过对这些数据的分析,可以获得程序的控制流和数据流信息,进而得到逻辑覆盖等动态信息,从而实现测试目的的方法。 On-the-fly插桩:JVM中通过-javaagent参数指定特定的jar文件启动Instrumentation的代理程序,代理程序在通过Class Loader装载一个class前判断是否转换修改class文件,将统计代码插入class,测试覆盖率分析可以在JVM执行测试代码的过程中完成。 Offline模式:在测试前先对文件进行插桩,然后生成插过桩的class或jar包,测试插过桩 的class和jar包后,会生成动态覆盖信息到文件,最后统一对覆盖信息进行处理,并生成报告。 注入方法 比较有特色的几个方法: Instrumentation 注入 Byte Code 字节码注入(在Byte Code时使用的ASM技术修改字节码方法

配置 mybatis 打印出执行的 sql 及返回的结果集

此生再无相见时 提交于 2020-12-03 01:25:01
在开发过程中, 经常会遇到想要看到应用所执行的 sql 这样的需求. 比如你写了一个查询的功能, 但查询出来的结果与你预期的不符合, 你想搞清楚到底哪里出了问题, 你自然需要看看所执行的 sql 语句, 必要的话甚至还要亲自拷贝到数据库里去查查. 自然, 这就要求应用要能把执行的 sql 输出出来. 以常用的 mybatis 框架为例, 来看一个最终的效果: 14:48 ==> Preparing: select * from user where id = ? 14:48 ==> Parameters: 1(Integer) 14:48 <== Total: 1 另注: 这里的日志布局我启用了一种极简的风格, 只有"分钟:秒数", 具体见 配置简化开发阶段日志输出布局 的介绍. 那么, 在 mybatis 里, 这个要怎么做到呢? 配置 sql 输出 具体来说, 是要增加一个日志级别的配置, 将 dao(或 mapper) 包级别调整到 DEBUG . 示例: # log sql statement logging.level.net.xiaogd.sample.mybatis.dao=DEBUG 注: 上述配置建议放在你的本地开发环境配置文件中, 通常为 application-dev.properties, 关于 spring-boot 的 分环境配置 的 profile

How can Spring's test annotation @Sql behave like @BeforeClass?

核能气质少年 提交于 2020-12-02 07:10:11
问题 How can I tell the @Sql annotation to run only once for the class, and not for each @Test method? Like having the same behaviour as @BeforeClass ? @org.springframework.test.context.jdbc.Sql( scripts = "classpath:schema-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD ) public class TestClass { @Test public void test1() { //runs the @Sql script } @Test public void test2() { //runs the @Sql script again } } 回答1: You can't do that out-of-the-box. The @Sql annotation only has two