junit

Android Studio 3/Kotlin code coverage

孤街醉人 提交于 2019-12-18 21:19:11
问题 My android app is multi module project: include (android-app/kotlin-android)':application', (pure kotlin)':presentation', (pure kotlin)':domain', (android-library/kotin-android)':dataproviders' I'm using Junit/Mockito for tests and I have issue with generating code coverage for kotlin android modules only . Tested lines are visible for android studio. tested class in ui.viewmodel package: But, for pure kotlin (eg. domain, presentation) test coverage works fine: I'm using Android Studio 3.0

Test if another method was called

泄露秘密 提交于 2019-12-18 19:46:10
问题 So I'm sure there is something like this out there but I have been searching for an hour and haven't found exactly what I am looking for. say I have a class that looks like this: public class MyClass { public void myMethod(boolean shouldCallOtherMethod) { if(shouldCallOtherMethod) { otherMethod(); } } public void otherMethod() { System.out.println("Called"); } } How do I make something like this work? @Test public void shouldCallMethod() { MyClass myClass = new MyClass(); myClass.myMethod

Dealing with System.exit(0) in JUnit tests

僤鯓⒐⒋嵵緔 提交于 2019-12-18 19:39:38
问题 I am implementing some tests for an existing Java Swing application, so that I can safely refactor and extend the code without breaking anything. I started with some unit tests in JUnit, since that seems the simplest way to get started, but now my priority is to create some end-to-end tests to exercise the application as a whole. I am starting the application afresh in each test by putting each test method in a separate test case, and using the fork="yes" option in Ant's junit task. However,

Dealing with System.exit(0) in JUnit tests

試著忘記壹切 提交于 2019-12-18 19:39:23
问题 I am implementing some tests for an existing Java Swing application, so that I can safely refactor and extend the code without breaking anything. I started with some unit tests in JUnit, since that seems the simplest way to get started, but now my priority is to create some end-to-end tests to exercise the application as a whole. I am starting the application afresh in each test by putting each test method in a separate test case, and using the fork="yes" option in Ant's junit task. However,

How can I instantiate a Mock Kafka Topic for junit tests?

∥☆過路亽.° 提交于 2019-12-18 19:33:15
问题 I have some JUnit tests on code that uses a kafka topic. The mock kafka topics I've tried do not work and the examples found online are very old so they also do not work with 0.8.2.1. How do I create a mock kafka topic using 0.8.2.1? To clarify: I'm choosing to use an actual embedded instance of the topic in order to test with a real instance rather than mocking the hand off in mockito. This is so I can test that my custom encoders and decoders actually work and it doesn't fail when I go to

java中的assert关键字

这一生的挚爱 提交于 2019-12-18 19:09:27
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 因为我们平时不会用java自带的assert断言,基本都是junit之类的成熟框架,之所以会想到这个,是因为在java9的ArrayDeque中看到的如下的源码: assert既然是为了调试测试程序用,应该不在正式生产环境下用吧,但是这个源码中为什么会有呢? assert使用的方式,有如下的方式: 1、assert <boolean表达式> 如果<boolean表达式>为true,则程序继续执行。 如果为false,则程序抛出AssertionError,并终止执行。 2、assert <boolean表达式> : <错误信息表达式> 如果<boolean表达式>为true,则程序继续执行。 如果为false,则程序抛出java.lang.AssertionError,并输入<错误信息表达式>。 当然默认jvm是没有开启断言的,比如Idea可以配置改成这样,eclipse估计也是差不多的,开关-enableassertions或-ea来开启: 比如我写个如下的例子: 输出: 来源: oschina 链接: https://my.oschina.net/u/2277632/blog/2879188

Inject Mocks for objects created by Factory classes

隐身守侯 提交于 2019-12-18 18:54:37
问题 I have the following class: public class MyClass { private Apple apple; public void myMethod() { apple = AppleFactory.createInstance(someStringVariable); .... .... .... } } And the Test class: @RunWith(MockitoJUnitRunner.class) public class MyClassTest { @InjectMocks MyClass myClass; @Test public void myMethod(){ ... ... ... } } How could I inject an Apple instance as a mock in MyClass? 回答1: You have 3 possibilities to solve this: Abstract factory : Instead of using a static method, use a

Does Maven surefire plugin run tests using multiple threads?

时间秒杀一切 提交于 2019-12-18 18:52:19
问题 I'm wondering if the Maven surefire plugin either runs tests multi-threaded by default (and if so can the number of threads be controlled? ) or if it runs tests from the Test classes in a random order or predictable order, or if the order can dictated by some means. I haven't verified this yet (I'll do so tomorrow just looking for some heads up guidance and verification at this point), but it looks as if my various JUnit Test classes are getting the tests run in some intermixed order. Which

Mockito mock all methods call and return

被刻印的时光 ゝ 提交于 2019-12-18 18:51:56
问题 I have a problem when writing unit testing with mock. There is a object which I need to mock have a lot getter, which I do call them at the code. However, those are not the purpose of my unit test. So, is there is a way I can mock all the methods instead of mock them one by one. Here is the code example: public class ObjectNeedToMock{ private String field1; ... private String field20; private int theImportantInt; public String getField1(){return this.field1;} ... public String getField20()

EasyMock void method

久未见 提交于 2019-12-18 18:49:44
问题 I'm trying to use EasyMock to mock out some database interface so I can test the business logic off a wrapping method. I've been going ok with methods that return by using the following in my setup of my test. DBMapper dbmapper = EasyMock.createMock(DBMapper.class); userService.setDBMapper(dbmapper); then within my actual test I run EasyMock.expect(dbmapper.getUser(userId1)).andReturn(mockUser1); EasyMock.replay(dbmapper); userService.getUser(userId1); This service then connects to the