Unit testing framework for my own android library - java

I'm building an android library for Android developers.
I want start unit testing my library and I'm looking for a good framework.
I saw many articles about Robolectric, but the problem is my library doesn't contain a lot of UI/Activities/Services or other Android elements.
I want to use Robolectric for regular java unit testing, and in not many cases where I need Robolectric for UI/Services test I'll use the Robolectic features.
My questions are:
1. Can I use Robolectric for pure java unit testing? And if I can, I will be happy to get a good tutorial for this.
2. Does Robolectric is the best framework for my needs?

The short answer is yes.
You can setup your project with robolectric & Junit4. Then, when you have tests that do not touch any Android-specific bits, you can just run them as normal JUnit tests. When you need Robolectric, you use the Robolectric test runner. The runner you use is what makes the difference.
You run them all as JUnit tests anyway, so your entire library tests out with one run.

Related

Run Junit test in several threads. Can Gatling execute JUnit tests?

I need to run existing functional autotests in several threads same time (number of threads will be set in param ) with the aim of load testing.
Functional autotest is written in Java with JUnit and is launching from Jenkins.
Is there any way to implement this task without recording the scenario in the Jmeter?
I heard about the possibility of running tests written in Java in the Gatling framework.
Any ideas?
Yes you can do it using library https://github.com/encircled/jPut. This is exactly what you are describing: using unit tests for perf/load/stress testing with easy integration with CI pipelines thanks to JUnit. Tests itself can be written in any JVM language.

Android Studio JUnit pure Java tests

Usually, when developing algorithms for android applications in eclipse, I used pure Java projects that would have a dependency set to the android project and could then run JUnit tests in the pure Java classes. This had great advantages since I could run my tests on my algorithms and logic classes really quickly without any deploying.
Could anyone tell me if it is possible (and how) to do something similar with android studio??
Thanks!
How about the gradle android test plugin?
("A Gradle plugin which enables good 'ol fashioned unit tests for Android builds." sounds like what you want)
Or maybe something like robotium / roboelectric.

Automated testing tool for java post development

I have an existing java application which is developed in Netbeans this is my first major development project so i didn't think about the use of log4j & junit in first place(A good lesson learnt). since now i am at the end of the project i miss these two . is there is any tool or jar which can create automated testing & logging with minimum effort ? I guess Adding log4j is easy but what about junit ?
There is nothing to say that you cannot use Junit after you have created a project. It means that you are not making use of test driven development, but there is no reason why that is an issue once you have already created your project.
I would recommend the netbeans tutorial on exactly how to do that:
https://netbeans.org/kb/docs/java/junit-intro.html

Regular JUnit in an Android project

I've been trying to figure out how to include standard JUnit 3.x (or even JUnit4.x) tests in my Android applications. I want to use JUnit over the Android unit test tools because it is faster and runs directly in my IDE (where the Android JUnit tools need to be deployed to the device or emulator 1st). I also want to use JUnit to impose proper SoC (Separation of Concerns). Originally I had set up multiple modules to allow for this, the Main Android module and a core library module where the main module would have all of the Android specifics and the core module would have Plain Old Java Objects (POJOs) and JUnit goodness.
Recently I started working on an Android project where I developed a library to handle some low level android specific work and expose it as high level operations to the main module. The problem is that I need to use JUnit in the library module but I don't want to break out yet another module to do so. In an ideal world I would have:
Main module: includes all the high level UI and controller logic to navigate between activities, fragments, etc. Depends on MyLib.
MyLib: Contains high level interfaces wrapping low level logic to handle things such as android specific networking, persistence, accelerometer reads, etc. Lots of POJOs involved in some of the low level logic. Also includes an example or demo app to exercise these interfaces directly. Includes the ability to run JUnit on the POJOs.
Am I living a pipe dream or is there a way to intelligently set this up using current development tools? I use IntelliJ Idea primarily but would be open to an Eclipse specific solution as well.
The problem you will run into with running your tests directly in the JUnit test runner is that you will not be able to call any com.android.* classes. That is because these classes, in android.jar, are stubbed out in your SDK. Only the Dalvik- byte code versions of these have actual implementations those only run in the emulator.
Avoiding calls into com.android.* classes is very difficult, especially given that so many Android methods require a Context.
I too find this a bit frustrating because the emulator is slow to launch and slow to execute code. It makes test development very tedious.
The only luck I have had using the JUnit test runner w/out the emulator is to create a separate, non-Android project that points at my project files. This allowed my to launch JUnits outside of the emulator, however I was very limited in what classes I could actually test.
In short
Use Junit for testing classes and static methods.
Use Instrumentation to test the click-ux response of your app
Instrumentation is a pain to get started, takes time to learn. So invest time in hello worlds a lot. Its a whole new world.

What is the standard for testing in Java?

What apps would you use? Are there auto testing suites like autotest for ruby? What do you use and why? To be honest, I don't even know how to write tests, when, or why. I'd like to learn though, I know that it will make me a better developer.
Our team uses Netbeans, not eclipse, although I'm going to still google eclipse responses to see if they are implemented as a Netbeans solution as well.
There are 2 most popular frameworks for unit tests: JUnit and TestNG. Both are annotation based. To create test you have to create class and mark each method that performs test using annotation #Test.
JUnit is older and have more extensions (DBUnit, Cactus etc). TestNG has much more annotations. Very important feature of TestNG is ability to create test groups using annotations.
Yet another group of tools you will probably need is mocking tools (EasyMock, EasyMock etc.)
There are a bunch of testing frameworks that are popular. JUnit is pretty good and comes by default with Eclipse. It provides an API for defining tests and doing assertions, as well as a Testrunner to execute the tests. EasyMock and Mockito work well with JUnit to provide mocking functionality so you can test components in isolation.
For continuous integration, there is Jenkins, which is free.
There are others as well.
I would use junit and possibly a mocking library like jmock.
Most of the automatic "tests" which can be done use the compiler or a code analysis tool like FindBugs.
In addition to what has already been said (JUnit, EasyMock, ...) you may also have a look at Fitnesse: it may be a good tool for full integration and acceptance tests!
Don't forget TestNG. It's the "next generation" beyond JUnit. It handles threaded tests better.
SOAP UI is the right tool for testing SOAP web services.
JMeter or Grinder for load testing.
As JUnit and Mockito was already mentioned, You can look into Infinitest or JUnit Max for autotesting.
http://infinitest.github.com/
http://junitmax.com/
If you are looking for something that implements continuous testing I can recommend two free products:
For a developer during work in Eclipse/IntelliJ IDE:
http://infinitest.github.com/
Infinitest is an Eclipse/IntelliJ plugin that runs your test continuously in the background while you are developing your code.
For a team:
http://hudson-ci.org/
or
http://jenkins-ci.org/
are great continuous integration servers that can do builds and run tests continuously.
Been writing junits for over 7 years now and I highly recommend spock for all your testing needs: unit and integration testing, mocking, end-to-end testing, data driven testing etc

Categories

Resources