Showing posts with label test. Show all posts
Showing posts with label test. Show all posts

24 May 2017

How run test multiple times in IntelliJ IDEA ?

Solution works for :  IntelliJ IDEA 2016.3 and later, but I don't know when it was added.

Ages ago, IntelliJ IDEA added really cool feature to repeat test.
If for whatever reason you need repeat test x amount of times then follow these steps:

  1. Run test (tests) first.
  2. Go to Edit Configuration
  3. In configuration tab, in top right corner, you can specify repat test


Apply and run it :)

Small warning!  For very large amount of test IntelliJ IDEA start works very very slowly. I usually I don't have problem with running more than around 65535  and more tests,but it depends on tests amount and size of the project :).



5 September 2016

How to test method that uses System.out.print ?

Did you ever need to  System.out.println ?
 Quite unlikely? Well I am not surprised ,  because you never use it this kind of thing in production. However sometimes when you write some personal piece of software,you don't bother to use debugger or logging and you fancy to use System.out.println instead.

However as TDD fanatic you would love to test it but question is how ?
Well, it was something that I was curious about it and after doing wide search on internet and I found that in order to test it you need  set your own printstream, how see example below :).

Message.java
public class Message {
    public static void error(String errorMessage) {
        System.err.print("ERROR: " + errorMessage);
    }
 
}

MessageTest,java
public class MessageTest {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PrintStream original = System.err;


    @Before
    public void setUp() throws Exception {
        System.setErr(new PrintStream(outputStream));
    }

    @After
    public void tearDown() throws Exception {
        System.setErr(original);
    }

    @Test
    public void displayErrorTest() throws Exception {
        //Given
        final String errorMessage = "Test";

        //When
        Message.error(errorMessage);

        //Then
        assertEquals("ERROR: " + errorMessage, outputStream.toString());

    }
}

I hope this help

23 October 2015

What I learnt from 006: Mock services,Not objects

I went to a talk "Mock Services, not Objects" presented by Tom Akehurst who is an author of WireMock.
Event was organized by London Java Community (LJC). 

WireMock:
"WireMock is a flexible library for stubbing and mocking web services. Unlike general purpose mocking tools it works by creating an actual HTTP server that your code under test can connect to as it would a real web service. It supports HTTP response stubbing, request verification, proxy/intercept, record/playback of stubs and fault injection, and can be used from within a unit test or deployed into a test environment." Source: http://wiremock.org

Tom Akehurst:
"Over the last decade or so I’ve been a developer, app support lead, scrum master and architect. I’m a strong advocate of agile, DevOps and Continuous Delivery practices, and am usually focussed on improving team performance and building operable, sustainable systems." Source: http://www.tomakehurst.com/about/
Rob Elliot is a developer who works with Tom for over decade. I don't know more about him . (Sorry :( )

Talk starts from ...fire alarm , where ...automatic system said "there is a fire alarm activated in ... (silent)" . Yeap, it didn't say where is fire. Luckily , a few minutes later we had an another announcement "There is no fire.Everything is OK. It is security guard".

Tom talked about motivations behind WireMock (like architecture changing from monolith to x amount of smaller services ,  team structure changed to many small teams and so on)
 and how he and other users used his tool on daily basis. He explains what features are useful and when use them and where to not use them. It talks about a few useful design patterns in tests.  Rob did quick live code demo where he shows an example of how WireMock works in practice . I really like it this demo as it shows a high level picture of how things works in practices. Whole talk and demo was nice , informative and simple. It was a very useful talk for people who are interesting in testing.

What I learnt :
  • What is WireMock and why it can be useful . My favourite functionality were record/playback feature , that can come handy in many tests, it can proxy network so it allows you test application against various cock-ups caused by network issues (failures and faults).  It is useful in  stubbing and verification.
  • WireMock is useful tool for test application behaviour when network causes problem.
  • Another proof why Builder pattern is extremely useful  for create flexible data for test. It seems  almost mandatory pattern when you write tests. For example you can Generate test data via builders on setup.
  • Data Randomness is useful as it helps detected edge and unexpected cases.
  • Documentation for WireMock is an amazing . It is nice balance of quick information of what feature does and code example .
Authors mentioned few tools:
  • Sham- test data generation library
  • Fastly.com is a Fastly's real-time CDN (A content delivery network (CDN) is a system of distributed servers (network) that deliver webpages and other Web content to a user based on the geographic locations of the user, the origin of the webpage and a content delivery server. Source: www.webopedia.com/TERM/C/CDN.html )

Networking after Meetup was useful ,because I have opportunity to talk about pair programming and various stuff about testing.

WireMock can be find here : http://wiremock.org

More about LJC can be found here: http://www.meetup.com/Londonjavacommunity/

10 April 2015

Doms diagnostic tools v5.3 released!

Doms diagnostic tools
v5.3


 You can download from here:

I added support for new version for Android and fix 

CHANGES:
  1. add support for Android 5.1 and all screen
  2. update battery info
  3. fix keyboard issue
  4. fix nfclab appear in menu
  5. fix bt issues
  6. clean a code and various refactoring

Twitter: 
Blog:

8 March 2015

Doms diagnostic tools v5.2 released!

Doms diagnostic tools
v5.2

 You can download from here:

I am focus on improvements of existing tests and add new information 

CHANGES:

  1. add more informations in general info in informations
  2. add more information in telephony info in informations 
  3. minor improvements
  4. fix minor bugs



3 March 2014

Doms diagnostic tools v4.4 released!

Doms diagnostic tools
v4.4

 You can download from here:

Fix some bugs

Changes:

  1. # fix java.lang.IndexOutOfBoundsException
  2. # fix problems  with General Info 
  3. # flashlight bugs
  4. # vibration UI issue
  5. # fix minor fixes

19 February 2014

Doms diagnostic tools v4.3 is released.

Doms diagnostic tools
v4.3

 You can download from here:

Improve UI and add few little things

Changes:
  1. Add stereo to audio test
  2. Simplify UI (however work still in progress)
  3. improve results information on the end of test
  4. Fix few ANR issues (however most of them are unresolvable as problems was caused by Android OS issues).
  5. Fix java.lang.IllegalStateException issue in Audio Test
  6. Fix java.lang.NullPointerException in Sensor Scanner
  7. wifi Scanner
  8. add warning about brightness
  9. Summary contains information from General and Phone Info
  10. improve mutlitouch
  11. clean code and other minor fixes

19 November 2013

Doms diagnostic tools v4.2.3 is released.

Doms diagnostic tools
v4.2.3

 You can download from here:

Quick bug fix release .As user loves switch off screen and panic,so i decided to add automatic solution

Changes:
  1. brightness back to medium brightness when user left brightness test(so even,if user switch off lights,it can recover from this mistake : ) .Thank you Steve for feedback.

9 November 2013

Doms diagnostic tools v4.2.2 is released.

Doms diagnostic tools

 You can download from here:

This is an improvement  release.I focus on improve stability of application and feature like jack plug detection..

Changes:
  1. add detection for Android 4.3 and Android 4.4
  2. add wifi scanner
  3. detection of sqllite version used on your device
  4.  improve wifi test 
  5. Jack plug detection
  6. update admob
  7. update appBrainImprove battery information
  8. remove unused code
  9. add GPS Test to full diagnose.Thanks to Diaman Z for feedback
  10. fix minor bugs
  11. add commander,so some user can run experimenal feature

8 July 2013

Doms diagnostic tools v4.2.0 is released.

Doms diagnostic tools


This is a quite big bug fix release.I focus on improve stability of application and some improvements 'behind the scenes' to improve overall functionality.

Changes:
  1. Update AppBrain
  2. Update about me.
  3. Improve brightness test
  4. Major redesign , refactoring  behind the scenes (for stability and improvements reasons)
  5. Application can be install internal storage only.
  6. Fix shameful android.util.Log$TerribleFailure 
  7. Fix minor bugs, correct mistakes and etc.
  8. Advice for temperature appear only,if temperature sensor is on device.(before it always displayed)
  9. Few fixes for wifi test.I temporary blocked remove all wifi networks ,but they will back in next release
  10. Add admob

15 February 2013

How to make simple game in Android part 7. - Testing

WARNING!
Few honest informations.  
I have a few years experience in software developer BUT  I am NOT a game developer! I am NOT an expert. 
This guide was written by person who want write game as part of hobby and make dreams come to true (As i want to write simple RPG game in future).   
 I put best efforts to write  accuracy informations, but I cannot guarantee the perfect accuracy of all content in this  articles  on this blog.
I do not take any responsibility or liability for any error, omission , inaccuracy
or destructions!

How to make simple game in Android


BASED ON GAME EXAMPLE: 
DOMS COUNTRIES, CAPITOLS, FLAGS AND CONTINENTS QUIZ GAME

 TO DOWNLOAD GAME

Part 7 
TESTING

I will be honest. I didn't write any JUnit tests and I haven't done proper test. Shame on me!

I didn't see too much sense for write  tests for quick project with extreme time constraint like 'writing small application in weekend'. I believe in this type of cases writing a tests can be a too much hassle and it was a too much hassle for me. 

I wrote some test for my applications that tests core functionality, but I am not TDD-fanatic (TDD stands for Test Driven Development, and it is a software development process, where you write a test, before you even write code to meet its purpose. If want know more read quite cool article http://www.agiledata.org/essays/tdd.html).
I am not good in writing test for android application as I have problem with find and write a meaningful tests. (I am aware, that is my weakness I need to improve this).

I don't write simple test case like for sets/gets methods and other obvious basics as I believe is waste of time, but I assume that TDD-fanatic will don't agree with that. Depends on your software development methodology and styles, You should decide.. .How many test you planning to write.
Specially in games is useful to have debug mode, where you can test how game behave in specific situation.

So what I should test in 'doms countries capitals and flags quiz game'?
Here you can find my ideas for test:


High scores where you can test:
  •  place and scores are correct.
  • Is sorting for scores works  
  • (TDD people will test add and remove score)


In Level You can test:
  •  is level 4 unique answer,
  • is 50 questions are unique


Countries:

  • you can test is loading file loaded all
  • is data is correct
  • amount of countries is correct

As you can see my code is not well written as is hard to test many method are void (doesn't have return value).Make sure,that when you writing 
 I have done 2 types of 'sort of' tests:
I validate date in load countries from file and load/save high scores from file
and I have done old-school empirical tests (run app and see, is it doing a right things in specific scenarios)




Validation helps me test app and check is new countries added to list are correct
This is example of validation for country

public boolean isValid() {

if (id <= 0) {

return false; //as id is start from 1

}
if (!DomUtils.isEmpty(countryName)) {
return false;
}
if (!DomUtils.isEmpty(country)) {
return false;
}
if (!DomUtils.isEmpty(capital)) {
return false;
}
if (region == null) {
return false;
}
return !DomUtils.isEmpty(flagId);
}
}
Note: Incoming data is already checked against is data's correct type (String, int and etc.).


What is a DomUtils? It is my selection of common used Java methods.
I found it, that have own collection of common used methods classes speed up your development.
I wrote my utils, which looks like Apache Common.


Tip: If you want improve your programming skills Apache common source code is a great place, to learn, how to write better quality code.




public static boolean isEmpty(String string) {


return (string == null || string.equals(“”));


}


Conclusions:
  1. Test are important!
  2. You should always write some tests specially for core functionality and places that dealing with validation from user input or files that are modified on regular basis.
  3. If you like TDD, then you must write test everything, before you write code.
  4. It can be a difficult to start. My first adventure with Junit was quite painful. Read about my biggest fail in development blog entry: http://pastorcmentarny.blogspot.co.uko.uk/2011/11/fail-development.html
  5. Do NOT ignore writing test. They will saves you from hot fix releases and 1 stars reviews.


Note: I will update this part, when I add some tests.

Emulator vs Device


Do not use Emulator!
Yes. It is possible to write game using emulator only.
I know one example of game, that was written this way and it works: (Made by  diskthrasher, link  https://play.google.com/store/apps/details?id=uk.me.diskthrasher.monkey),
but believe me you will save 50% of your precious time on development on mobile!


Buy any modern Android device that cost around £100/$150
As early 2013,Minimum which will cover
Android 4.x
CPU:  1Ghz (800Mhz  minimum),1 Core
800×480 (or even 480×360)
and it will be enough for while.


Remember! Testing in emulator  is a very painful process... .You can write a game without using emulator. However, if you want enjoy develop application for Android. BUY SMARTPHONE or cheap TABLET and DO NOT USE EMULATOR!!


That's it for testing.In next part I will give few tips about what to prepare before you add your project into Google Play.
Go to : Add project to Google Play





















18 July 2012

How to install TestNG in STS (and Eclipse)

Solution works for Eclipse 3.5/STS 2.6.0 (According to TestNG it works from Eclipse 3.4)

Bored with JUnit ??? Try TestNG .

I didn't find anything overexcited about TestNG  but it has few interesting features that JUnit doesn't have or it is easier to use in TestNG.

In STS:

1. pres Help in menu.
2. Install new software
3.  in URL field type : http://beust.com/eclipse and click Add (STS will ask you to add name... choose what you want,be if you creative is not your strong point type: testng)
4. Select testng
5. Press nexts  as many times as it required (You need  agree for terms  and agree that you use unsign stuff)
6. Press finish
7. restart STS , Eclipse
Done it! You can now have a legendary tests with TestNG

if you need quick tutorial .Use mykong's  website that contains basic tutorial:
http://www.mkyong.com/tutorials/testng-tutorials/

[UPDATE 1] 
*I strike first part of sentence as I need use spend more time with TestNG to have a proper opinion about it.
So far ,it didn't work perfectly well on Netbeans.I using at TestNG in Eclipse and it seems to not
Thanks to Tomek from  http://kaczanowscy.pl/tomek/node for interesting presentation (can be found in comment).

[UPDATE 2]
It is possible to install TestNG through Eclipse Marketplace* now and it is much easier!
Thank you for Olivier Billard's comment to inform me about it!

*  WOW . I am surprised that Eclipse Marketplace still exists and somebody actually used it .It is worst 'advertised' feature ever introduced. When I discovered  this (it was during reading some article about Java ,where autor of this post was surprised that .. it still exists and to make things more  funny most comments were " I didn't know that  Eclipse Marketplace exists ". Eclipse Marketplace sounds like great idea,but implementation and advertising was in Eclipse style.