jmockit

Mock a private static field with JMockit?

筅森魡賤 提交于 2020-08-06 07:53:18
问题 I have a class like the following; class ClassA { private static File myDir; // myDir is created at some stage private static String findFile(final String fileName) { for (final String actualBackupFileName : myDir.list()) { if (actualBackupFileName.startsWith(removeExtensionFrom(backupFile))) { return actualBackupFileName; } } } } So, basically, I want to test this class by mocking out the File class so that when list() is called on it it returns a list of strings I define in my test class. I

【IDEA】单元测试:项目中引入JUnit测试框架+Mock简单了解

风格不统一 提交于 2020-04-30 03:21:15
一、Junit 使用和说明: 参考:单元测试第三弹——使用JUnit进行单元测试-HollisChuang's Blog http://www.hollischuang.com/archives/1760 1.1 主要内容 如何使用IDEA上手JUnit,以及如何简单测试。 JUnit中的Assert的方法,一共6个:assertEquals,assertFalse,assertNotNull,assertNull,assertTrue,fail JUnit中的注解,一共6个:@BeforeClass –> @Before –> @Test –> @After –> @AfterClass 总的来说,对于Junit的理解,这一篇文章就能掌握基本使用了。 1.2 实践说明以及注意事项: 一般对于IDEA来说,创建的项目中已经自动导入了 Junit 的Jar,如果没有,自行在pom文件中导入即可; IDEA如果想运行单元测试,好像必须把单元测试所在的根目录标记为 Test Resource Root才可以。标记之后,在方法前加上 @Test 注解之后,方法体里右键,就会有运行该方法的选项。 如果想要运行整个单元测试类中的所有方法,请不要把鼠标放在 @Test 注解的方法内部右击鼠标。 这里说明一下:一般的maven项目中,会在src/main下面有两个目录,java和test

使用Groovy进行Java的单元测试

▼魔方 西西 提交于 2020-04-26 06:06:47
为什么用Groovy写单元测试 它与Java 平台无缝的集成,它是基于 Java 的语言(不像其他语言,是对 JRE 的替代,因此可能基于旧版的处理器),对于 Java 开发人员来说,Groovy 意味着一条短得让人难以置信的学习曲线。而且一旦将这条学习曲线拉直,Groovy 就能提供一个无与伦比的快速开发平台。 从这个角度来说,Groovy 成功的秘密,在于它的语法 就是 Java 语法,但是规则更少。例如,Groovy 不要求使用分号,变量类型和访问修饰符也是可选的。而且,Groovy 利用了标准的 Java 库,这些都是您已经很熟悉的,包括 Collections 和 File/IO 。而且,您还可以利用任何 Groovy 提供的 Java 库,包括 JUnit。 事实上,令人放松的类 Java 语法、对标准 Java 库的重用以及快捷的生成-运行周期,这些都使 Groovy 成为快速开发单元测试的理想替代品。 如何使用Groovy进行Mock测试 用Groovy进行Mock要比JMockit之类的简单很多,下面举例还演示如何使用 待测试类 折叠源码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

How to inject mocked dependencies with jmockit

落爺英雄遲暮 提交于 2020-01-31 09:05:24
问题 Currently I try to understand how the @Injectable and @Tested annotations are working. I already did some tests and understood the concept but I didn't get how I can use those annotations in real world applications. Let's say we are developing a language translator class which depends on a web service. The web service methods are encapsulated in a separate class: // class to test public class Translator() { private TranslatorWebService webService; public String translateEnglishToGerman(String

How to inject mocked dependencies with jmockit

人走茶凉 提交于 2020-01-31 09:05:16
问题 Currently I try to understand how the @Injectable and @Tested annotations are working. I already did some tests and understood the concept but I didn't get how I can use those annotations in real world applications. Let's say we are developing a language translator class which depends on a web service. The web service methods are encapsulated in a separate class: // class to test public class Translator() { private TranslatorWebService webService; public String translateEnglishToGerman(String

JMockit: @Mocke and MockUp combination in the same test

二次信任 提交于 2020-01-16 02:55:08
问题 What I have to do: I have to test my spring mvc with JMockit. I need to do two things: Redefine MyService.doService method Check how many times redefined MyService.doService method is called What the problem: To cope with the first item, I should use MockUp ; to cope with the second item I should use @Mocked MyService . As I understand this two approaches are overriding each other . My questions: How to override MyService.doService method and simultaneously check how many times it was invoked

How can I check a specific mocked instance is passed using JMockit?

百般思念 提交于 2020-01-15 06:10:47
问题 I want to test the following code BufferedReader reader = new BufferedReader(new FileReader(args[0])); If I write a test like this, it works @Mocked FileReader fileReader; @Mocked BufferedReader bufferedReader; //... new NonStrictExpectations() {{ new FileReader("filename"); times = 1; new BufferedReader(withAny(fileReader)); times = 1; }}; However, this test does not make sure that the create FileReader is passed to the ctor of BufferedReader, only that a FileReader gets passed. What I

How can I check a specific mocked instance is passed using JMockit?

本小妞迷上赌 提交于 2020-01-15 06:08:19
问题 I want to test the following code BufferedReader reader = new BufferedReader(new FileReader(args[0])); If I write a test like this, it works @Mocked FileReader fileReader; @Mocked BufferedReader bufferedReader; //... new NonStrictExpectations() {{ new FileReader("filename"); times = 1; new BufferedReader(withAny(fileReader)); times = 1; }}; However, this test does not make sure that the create FileReader is passed to the ctor of BufferedReader, only that a FileReader gets passed. What I

JMockit + Jetty in functional tests

江枫思渺然 提交于 2020-01-02 04:32:29
问题 I'm using ShrinkWrap to start Jetty server in my integration tests. Problem: When I start my test jetty-server and than make mockup of my controller - mockup doesn't work! I suggest that the reason is different classloaders: JMockit - AppClassLoader, Jetty - WebAppClassLoader. Question: How to make mocking works fine? P.S. I've googled that -javaagent:jmockit.jar option may help. But it doesn't. Is it necessary for maven project based on 1.7 jdk? ADDITION: I've written demo to illustrate my

Mock private method in the same class that is being tested

半世苍凉 提交于 2019-12-31 10:44:05
问题 I have a Java class named, MyClass , that I want to test with JUnit. The public method, methodA , that I want to test calls a private method, methodB , in the same class to determine which conditional path to follow. My goal is to write JUnit tests for the different paths in methodA . Also, methodB calls a service, so I do not want it to actually be executed when I run the JUnit tests. What is the best way to mock methodB and control its return so that I can test different paths for 'methodA'