Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for PackageManager #72

Closed
vidstige opened this issue Nov 2, 2017 · 17 comments
Closed

Unit test for PackageManager #72

vidstige opened this issue Nov 2, 2017 · 17 comments

Comments

@vidstige
Copy link
Owner

vidstige commented Nov 2, 2017

Looking at the code coverage over at codecov.io, I see I've missed to add unit tests for the PackageManager class. See more here: https://codecov.io/gh/vidstige/jadb/src/39dc7ee90f31bd97bba523ebb800ff2cf1f6621c/src/se/vidstige/jadb/managers/PackageManager.java

This should be easy to add, glancing at the already existing unit test.s

@Jack-William
Copy link

Jack-William commented Nov 3, 2017

Hi ! I'm new , I'm trying to understand how work the big world of GitHub.
Can I try to resolve this issue ? what can I do ?

@vidstige vidstige assigned vidstige and unassigned vidstige Nov 3, 2017
@vidstige
Copy link
Owner Author

vidstige commented Nov 3, 2017

Yes, please, that would be very much appreciated. This is a matter of adding a new unit test fixture class with a few unit tests. Let me ask you some questions about your background. Have you worked with unit tests before? How familiar are you with Java?

@Jack-William
Copy link

I'm studying Java at university, but at the moment, we haven't talk about the unit tests classes. So I've never worked with these type of classes.

@vidstige
Copy link
Owner Author

vidstige commented Nov 4, 2017

Alright. It's going to be fine. You'll learn a lot. What editor do you use? I recommend IntelliJ.

  1. Clone repo
  2. Open in IntelliJ. I think you import maven project.
  3. Run existing unit tests.

Let me know when you came thus far, or if you run into any snags. 😊

@Jack-William
Copy link

I've done it ! So the tests classes are classes where I try my main
application , it's correct ? is a sort of tracing of more high level ?

@vidstige
Copy link
Owner Author

vidstige commented Nov 7, 2017

Great! :-) And the tests are green?

So jadb is a library, so there is no real main application. This is where the unit tests come in. The unit tests mock out actually talking to an android device, but tests everything else, e.g. that the protocol is correct and so on.

Real use scenario

Application (in another repo) -> jadb -> Android device

Unit test

Unit test -> jadb -> Mocked device

This has the benefit of not having to have an Android device connected when running the test, I can run then automatically whenever a code change is committed. See more here. Furthermore, the unit tests acts as examples on how to use the library.

The next step is to create a new unit test. Easiest will be to just add a new function to the MockedTestCases.java file. You can start by adding a new function and make sure you can run it from within IntelliJ, then just to try it, make it fail by raising an exception in the function body.

Next step is to create an instance of the PackageManager class we want to test, and calling a function on it, e.g. getPackages().

The way this works is that the package manager just invokes a program on the Android device called pm. For the unit test cases the actual device is mocked. You will need to control what will happen when the pm program is started on the mocked android device. See here for example on how to control the what a program will output when invoked on the mocked device. For the test to pass it will need to output something in the same format as the real pm program. If you have an android device yourself, you can try it on the android device and see what the pm program outputs, otherwise we can infer it from the getPackages() code, I can help you with this.

Does this all makes sense? Let me know if you have any questions regarding unit tests, or what to do next or if you run into and snags.

@vidstige vidstige assigned vidstige and unassigned vidstige Nov 10, 2017
@Jack-William
Copy link

Not all the tests are green, 15 test are ignored yellow color.
I'm trying to create a new unit test but where I can see the new functions to add ?
Thanks for the explanations :-)

@vidstige
Copy link
Owner Author

vidstige commented Nov 10, 2017

Ok, that might be the integration tests... That's fine.

You just write the code for a new function and re-run the tests. You should see the test name appear

Try something like this

@Test
public void testwilliam() throws Exception {
}

@Jack-William
Copy link

Jack-William commented Nov 10, 2017

Yes, I've written in this mode, but I don't understand what to put in the method block. I leave it empty?

@vidstige
Copy link
Owner Author

Well, it's a process so empty body is a good start to get you going. Before this issue is done we want to test the PackageManager class. So we need to create an instance if this class, and call a method. And then assert something.

@vidstige
Copy link
Owner Author

Maybe you should try a tutorial to learn about unit testing first?

@Jack-William
Copy link

Step by step, I understand ! :-)
I've created a new empty method and re-run the tests. I've found my test name, it was green and the message associated was :
Starting fake on port 15037
Stopping fake on port 15037

@vidstige
Copy link
Owner Author

Great! 😊 And do you feel you have a way forward?

@Jack-William
Copy link

I'm trying! :-)
I've raised an exception for verify if also the exception management go well with the method that I've created

@vidstige
Copy link
Owner Author

vidstige commented Dec 5, 2017

Any news? :)

@bcallebaut
Copy link

@vidstige : Could that be issue marked as resolved ?
According to the pr it seems to be solved (at least for the biggeest part)

@vidstige
Copy link
Owner Author

right, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants