junit

How do I write an android JUnit test when my activity relies on extras passed through an Intent?

三世轮回 提交于 2020-01-02 00:13:29
问题 I am writing an android Junit test for a class that relies on extras passed to it through an Intent. I was able to get the class working properly, but I would still like to know how to write a unit test for such a class, as the test still fails. public class AddClassEvent extends Activity{ private String eventType; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle extras = getIntent().getExtras(); final String cNo = extras.getString(

Junit - Multiple @Before vs. one @Before split up into methods

随声附和 提交于 2020-01-01 23:11:53
问题 In a unit test, I need to perform a quite complex setup (this may be a code smell but this is not what this question is about :-)). What I'm interested in is if it is better to have multiple @Before methods performing the setup or just one, which calls helper methods to perform the initialization. E.g. @Before public void setUpClientStub() { } @Before public void setUpObjectUnderTest() { } vs. @Before public void setUp() { setUpClientStub(); setUpObjectUnderTest(); } 回答1: As has been said in

Why Json ask for no argument constructor for Junit test?

风流意气都作罢 提交于 2020-01-01 19:49:10
问题 I have a problem deserializing a JSON string using Jackson. I am getting Error : com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class RatingDMO]: can not instantiate from JSON object (need to add/enable type information?) I have the fields mapped to the JSON file and parameters to it are available as properties AND have the same type. Note :- If I provide the no-arg constructor(just for checking if it works), the Test works. I can't

Failed to load ApplicationContext for JUnit test of Spring

末鹿安然 提交于 2020-01-01 18:19:46
问题 I'm writing test class with @ContextConfiguration . But I'm getting java.lang.IllegalStateException: Failed to load ApplicationContext error. File Structure project *src/main java com.abc.controller abc.java resources sample-client.xml rest-client.xml *Test com.abc.client Client.java Test class Client.java @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath*:/sample-client.xml"}) public class Client { @Resource(name = "facebookClient") FacebookService

Reflectively get all packages in a project?

蹲街弑〆低调 提交于 2020-01-01 17:30:13
问题 How can I reflectively get all of the packages in the project? I started with Package.getPackages(), but that only got all the packages associated to the current package. Is there a way to do this? 回答1: @PhilippWendler's comment led me to a method of accomplishing what I needed. I tweaked the method a little to make it recursive. /** * Recursively fetches a list of all the classes in a given * directory (and sub-directories) that have the @UnitTestable * annotation. * @param packageName The

How does Cobertura work with JUnit?

喜你入骨 提交于 2020-01-01 08:20:26
问题 I can't understand how Cobertura cooperates with JUnit. As I understood cobertura modifies compiled byte code and inserts in this byte code its own commands. Ok. After that we run Junit framework and give it our tests to run. Could anyone explain at what points cobertura gets the information which of its commands were executed? 回答1: Cobertura uses ASM which is a general purpose bytecode manipulation and analysis framework. On every line of java code there are 3 lines added to the existing

Compare scala.xml.Elem object in unit test

只愿长相守 提交于 2020-01-01 08:03:14
问题 I have two scala.xml.Elem objects (actual, expected). I am using JUnit 4, but have also included XMLUnit 1.3. Is there any easy way to compare the two objects for equality, ignoring attribute order and insignificant whitespace in the XML? I tried XMLUnit.assertXMLEqual() , but it complains that the types are scala.xml.Elem . I know that I can use equals or == , but I would like the benefit of having the assertion print the two values when they are not equal. If I use assertTrue(actual.equals

Compare scala.xml.Elem object in unit test

别等时光非礼了梦想. 提交于 2020-01-01 08:03:08
问题 I have two scala.xml.Elem objects (actual, expected). I am using JUnit 4, but have also included XMLUnit 1.3. Is there any easy way to compare the two objects for equality, ignoring attribute order and insignificant whitespace in the XML? I tried XMLUnit.assertXMLEqual() , but it complains that the types are scala.xml.Elem . I know that I can use equals or == , but I would like the benefit of having the assertion print the two values when they are not equal. If I use assertTrue(actual.equals

How to test Spring @Scheduled

陌路散爱 提交于 2020-01-01 07:41:12
问题 How do I test @Scheduled job tasks in my spring-boot application? package com.myco.tasks; public class MyTask { @Scheduled(fixedRate=1000) public void work() { // task execution logic } } 回答1: If we assume that your job runs in such a small intervals that you really want your test to wait for job to be executed and you just want to test if job is invoked you can use following solution: Add Awaitility to classpath: <dependency> <groupId>org.awaitility</groupId> <artifactId>awaitility<

How to test Spring @Scheduled

老子叫甜甜 提交于 2020-01-01 07:40:13
问题 How do I test @Scheduled job tasks in my spring-boot application? package com.myco.tasks; public class MyTask { @Scheduled(fixedRate=1000) public void work() { // task execution logic } } 回答1: If we assume that your job runs in such a small intervals that you really want your test to wait for job to be executed and you just want to test if job is invoked you can use following solution: Add Awaitility to classpath: <dependency> <groupId>org.awaitility</groupId> <artifactId>awaitility<