junit

How to test write to file in Java?

与世无争的帅哥 提交于 2020-01-21 00:42:26
问题 I'm beginner, and keep yourself in hands. I have some easy program, and I need do junit test for write method. I have some collection in input. How I can do this? This my code: // 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) {

How to unit test a Spring MVC annotated controller?

*爱你&永不变心* 提交于 2020-01-20 14:21:48
问题 I am following a Spring 2.5 tutorial and trying, at the same time, updating the code/setup to Spring 3.0. In Spring 2.5 I had the HelloController (for reference): public class HelloController implements Controller { protected final Log logger = LogFactory.getLog(getClass()); public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.info("Returning hello view"); return new ModelAndView("hello.jsp"); } } And a JUnit

Is it possible to run incremental/automated JUnit testing in Eclipse?

烂漫一生 提交于 2020-01-20 02:43:45
问题 Eclipse support incremental compiling. If I save a source file then it will compile the modified files. Is it possible after such incremental compile also to run the JUnit tests of the same package and show the fail in the error view. Then I can see the JUnit test failing and compiling errors in the same view without extra action. Are there any plugins that can do it? 回答1: You have to look at these plugins: JUnit Max: Not free, developed by Kent Benk (one of the men behind the TDD practice);

spring boot junit controller

眉间皱痕 提交于 2020-01-19 10:01:24
MockMvc 来自Spring Test,它允许您通过一组方便的builder类向 DispatcherServlet 发送HTTP请求,并对结果作出断言。请注意,@AutoConfigureMockMvc 与@SpringBootTest 需要一起注入一个MockMvc 实例。在使用@SpringBootTest 时候,我们需要创建整个应用程序上下文。 示例代码: import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.annotation

junit及HDFS API常用方法

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-19 05:23:47
package com.haohaodata.bigdata; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.io.IOUtils; import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.URI; /** * Created by hager on 2020/1/12. * junit 及 HDFS API编程 * * 1、对于你要测试的方法,需要使用@Test这个注解 * 2、@Before和@After分别是在每个测试方式执行前后执行(即:每个测试方法都会执行一次@Before和@After) * 3、

一键自动生成 java junit 测试代码神器 gen-test-plugin 入门介绍

那年仲夏 提交于 2020-01-19 03:46:32
gen-test-plugin 纯爱小说 https://www.shupu.org/ 我们日常编写代码的过程中,经常需要为代码编写测试案例。 随着对代码质量的要求越来越高,很多公司开始通过代码的测试覆盖率作为 QA 的一个评定指标。 本框架可以一键生成所有代码对应的 junit 测试案例,为你的人生节约宝贵的时间。 特性 支持生成 junit4/junit5 支持 jdk7 支持自定义生成模板 更新记录 更新记录 gen-test 用于生成 Junit4/Junit5 单元测试。 引入 <plugin> <groupId>com.github.houbb</groupId> <artifactId>gen-test-plugin</artifactId> <version>0.0.1</version> </plugin> 属性说明 属性 说明 默认值 类型 备注 isOverwriteWhenExists 如果 test 文件已存在,是否覆盖 false 字符串 默认不进行覆盖 encoding 项目编码 utf-8 字符串 includes 包含文件正则 **\/*.java 字符串 默认为所有 java 文件 excludes 排除文件正则 字符串 默认不进行排除 junitVersion junit 版本 4 字符串 默认为 junit4 运行 命令行直接执行 mvn

Why doesn't this code attempting to use Hamcrest's hasItems compile?

纵饮孤独 提交于 2020-01-19 03:08:48
问题 Why does this not compile, oh, what to do? import static org.junit.Assert.assertThat; import static org.junit.matchers.JUnitMatchers.hasItems; ArrayList<Integer> actual = new ArrayList<Integer>(); ArrayList<Integer> expected = new ArrayList<Integer>(); actual.add(1); expected.add(2); assertThat(actual, hasItems(expected)); error copied from comment: cannot find symbol method assertThat(java.util.ArrayList<java.lang.Integer>, org.hamcreset.Matcher<java.lang.Iterable<java.util.ArrayList<java

Testing against Java EE 6 API

有些话、适合烂在心里 提交于 2020-01-18 21:35:53
问题 I write an addition to JAX-RS and included the Java EE 6 API as a Maven dependency. <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> Then I have a little test case: @Test public void testIsWriteable() { class SpecialViewable extends Viewable { public SpecialViewable() { super("test"); } } FreeMarkerViewProcessor processor = new FreeMarkerViewProcessor(null); assertTrue(processor.isWriteable(SpecialViewable

Testing against Java EE 6 API

北战南征 提交于 2020-01-18 21:35:30
问题 I write an addition to JAX-RS and included the Java EE 6 API as a Maven dependency. <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> Then I have a little test case: @Test public void testIsWriteable() { class SpecialViewable extends Viewable { public SpecialViewable() { super("test"); } } FreeMarkerViewProcessor processor = new FreeMarkerViewProcessor(null); assertTrue(processor.isWriteable(SpecialViewable

Testing against Java EE 6 API

人走茶凉 提交于 2020-01-18 21:35:28
问题 I write an addition to JAX-RS and included the Java EE 6 API as a Maven dependency. <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> Then I have a little test case: @Test public void testIsWriteable() { class SpecialViewable extends Viewable { public SpecialViewable() { super("test"); } } FreeMarkerViewProcessor processor = new FreeMarkerViewProcessor(null); assertTrue(processor.isWriteable(SpecialViewable