junit

Android JUnit test with Context.getApplicationContext()

不想你离开。 提交于 2020-06-13 06:54:31
问题 I am currently working on an android Room Persistency database project. I have successfully implemented all functions, and am now trying to write unittests for these function. These unittests rely on calling the following function (singleton constructor; I have marked the two important lines with comments): @Database(entities = {RippleModel.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract RippleModelDao rippleModelDao(); private static AppDatabase

Android JUnit test with Context.getApplicationContext()

ε祈祈猫儿з 提交于 2020-06-13 06:54:00
问题 I am currently working on an android Room Persistency database project. I have successfully implemented all functions, and am now trying to write unittests for these function. These unittests rely on calling the following function (singleton constructor; I have marked the two important lines with comments): @Database(entities = {RippleModel.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract RippleModelDao rippleModelDao(); private static AppDatabase

JUnit4 and JUnit5 tests not running in IntelliJ

不问归期 提交于 2020-06-11 17:09:13
问题 I am trying to use JUnit4 and JUnit5 tests in the same project in IntelliJ IDEA 2017.1.5. Until now, all tests were based on JUnit4. I added the jupiter , platform and vintage dependencies to my pom.xml (including the junit-platform-surefire-provider and junit-vintage-engine for the surefire plugin). Now, neither my example test for JUnit4 nor the one for JUnit 5 are executed. Instead, I get the following error: Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform

MockRestServiceServer: how to mock a POST call with a body?

微笑、不失礼 提交于 2020-06-10 03:46:11
问题 I am trying to mock a POST method with MockRestServiceServer in the following way: MockRestServiceServer server = bindTo(restTemplate).build(); server.expect(requestTo("/my-api")) .andExpect(method(POST)) .andRespond(withSuccess(expectedResponce, APPLICATION_JSON)); Problem: How do I verify a request body in this setup? I browsed through the documentation and some examples and still can't figure out how it can be done. 回答1: You can use content().string to verify body: .andExpect(content()

MockRestServiceServer: how to mock a POST call with a body?

我的梦境 提交于 2020-06-10 03:45:59
问题 I am trying to mock a POST method with MockRestServiceServer in the following way: MockRestServiceServer server = bindTo(restTemplate).build(); server.expect(requestTo("/my-api")) .andExpect(method(POST)) .andRespond(withSuccess(expectedResponce, APPLICATION_JSON)); Problem: How do I verify a request body in this setup? I browsed through the documentation and some examples and still can't figure out how it can be done. 回答1: You can use content().string to verify body: .andExpect(content()

JUnit Assert with BigDecimal

人走茶凉 提交于 2020-06-09 09:19:13
问题 I want to use assert between 2 two decimal, I use this: BigDecimal bd1 = new BigDecimal (1000); BigDecimal bd2 = new BigDecimal (1000); org.junit.Assert.assertSame (bd1,bd2); but the JUnit log shows: expected <1000> was not: <1000> 回答1: assertSame tests that the two objects are the same objects, i.e. that they are == : Asserts that two objects refer to the same object. If they are not the same, an AssertionError without a message is thrown. In your case, since bd1 and bd2 are both new

Not able to cover Catch block using Junits

大憨熊 提交于 2020-06-09 07:08:50
问题 I am trying to cover my Utility class using JUnits but i am not able to cover catch block since 1 day and i am not understanding what is problem for not covering this catch block can some one help me.. Class public class CustomStringConverter { private static final Logger LOGGER = LoggerFactory.getLogger(CustomStringConverter.class); private CustomStringConverter() {} public static String objectToJsonString(Object obj) { try { return new ObjectMapper().writeValueAsString(obj); } catch

How to run all modified JUnit test classes?

雨燕双飞 提交于 2020-06-08 07:09:20
问题 I have an IntelliJ project, versioned in git. How can I run all JUnit test classes, that I have modified since my last commit? 回答1: There is Changed Files predefined scope in Project tool window you can use to view all vcs changed files. From there select all JUnit classes and from the context menu create JUnit Run/Debug Configuration which will fill the classes pattern to run automatically. 回答2: I do not believe there is any 'out of the box' solution to this that you can use. However you

Wrong version of JUnit in dependencies

梦想的初衷 提交于 2020-06-01 03:15:53
问题 I want to run JUnit 5.4+ tests on my Spring Boot app so that I can use the @Order annotation on my tests. However, Maven resolves my POM to 5.3.2 regardless of what I try. I've tried including all the dependencies I can think of manually, but then I end up with a mess of mismatched versions. I also tried clearing my entire ~/.m2/repository folder and rebuilding the tree, same results. Relevant parts of mvn dependency:tree [INFO] +- org.junit.jupiter:junit-jupiter-api:jar:5.5.0:test [INFO] | +

Creating intent in test: “Method putExtra in android.content.Intent not mocked”

落爺英雄遲暮 提交于 2020-05-30 07:20:23
问题 I'm trying to unit test a broadcast receiver which listens for "com.android.music.metachanged" intents using JUnit4 and Mockito. The broadcast receiver starts a service when it receives an intent. I want to assert that the service is started. I also want to assert that the string extra "artist" of the received intent is the same as the one of the sent intent. This is what I have so far... @RunWith(PowerMockRunner.class) public class MusicBroadcastReceiverUnitTest { private