Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Use mock object to replace input in coverage in Modbat.scala #69

Open
cyrille-artho opened this issue Jun 19, 2019 · 9 comments
Open

Use mock object to replace input in coverage in Modbat.scala #69

cyrille-artho opened this issue Jun 19, 2019 · 9 comments
Assignees

Comments

@cyrille-artho
Copy link
Owner

Displaying path coverage is interactive to allow a user to select what parts of the coverage to display. This cannot be used with automated testing; the tests are currently disabled due to this.

We can use mock testing in case Modbat.isUnitTest is true.

See:
http://www.scalatest.org/user_guide/testing_with_mock_objects
https://scalamock.org/user-guide/advanced_topics/

The mock should replace readLine and return the right sequence of qs and other inputs.

@cyrille-artho
Copy link
Owner Author

Once we have the mocks in place, we can uncomment the corresponding tests in bin/test.sh again.

@wruiwr
Copy link
Collaborator

wruiwr commented Jun 26, 2019

In order to use mock, I need to add dependencies to Gradle
testCompile 'org.scalamock:scalamock_2.11:4.3.0'
testCompile 'org.scalatest:scalatest_2.11:3.0.1'

but scalamock is only allowed to import in test/scala, not in main/scala
In order to use mock in Modbat.scala (in main/scala),
I change dependencies to (not sure if this is the right way to do):
compile 'org.scalamock:scalamock_2.11:4.3.0'
compile "org.scalatest:scalatest_2.11:3.0.1"
then, I am allowed to import scalamock and scalatest in main/scala,
but after I created a class with mock
class PathGraphsBFSearch(val trie: Trie) extends FlatSpec with MockFactory {...} ,
and compiled with ./gradlew jar, I got a message that
there were two feature warnings; re-run with -feature for details one warning found
How can I use this -feature for gradle?

Also, I got en exception after run chooseTest with this search feature:

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: modbat/mbt/PathGraphsBFSearch at modbat.mbt.Modbat$.coverage(Modbat.scala:303) at modbat.mbt.Modbat$ShutdownHandler$.run(Modbat.scala:348)

Currently, I am not sure why it has NoClassDefFoundError.
But usually the scalamock is used in test/scala, and this is why it is suggested to use testCompile for the dependencies. Therefore I am not sure if this is the way to do the mock in Modbat.scala.

@wruiwr
Copy link
Collaborator

wruiwr commented Jun 26, 2019

However, I wrote a small program to test Mock as a fake readLine with the same way I did for modbat, and that program works fine...

@cyrille-artho
Copy link
Owner Author

Have you tried a build from scratch? Perhaps a dependency was wrong.
Otherwise, it seems fine to include the dependency on mocking with main. This is because you need the mock feature in main, as the application code itself uses a mock, so its code depends (at compile time) on the library.

@cyrille-artho
Copy link
Owner Author

If you have trouble getting mocking to work, you could also code this up explicitly:

if (Modbat.isUnitTest) {
  // perhaps use a counter and set input to "quit" after a number of counts
} else {
  input = readLine()
}

In that case, we don't need mocking. For this to work, you'll have to use the new unit test framework, which will be ready in a few days (I hope).

@cyrille-artho
Copy link
Owner Author

You can set "isUnitTest" to "true" by hand somewhere in the code in the meantime, to make this work.

@wruiwr
Copy link
Collaborator

wruiwr commented Jun 26, 2019

ok I will try this after I restart my laptop. It is too slow now...

@wruiwr
Copy link
Collaborator

wruiwr commented Jun 26, 2019

I fix this by using another configuration:
@Doc("test with user-defined search function of path coverage graphs")
var bfsearchFunTest: Boolean = false

So, if doing tests, we can set --bfsearch-fun-test to wait 6 seconds to quit , and if using readline, we can set --bfsearch-fun (without -test).

example:
#!/bin/sh
time scala modbat.jar --classpath=modbat-test.jar \
-n=100 \
-s=1 \
--log-level=debug \
--no-redirect-out \
--dotify-path-coverage \
--bfsearch-fun-test \
modbat.test.ChooseTest &> ChooseTest-n100.log

dot -Tpdf modbat.test.ChooseTest-root-StateGraph.dot -o ChooseTest-StateGraph-n100.pdf
dot -Tpdf modbat.test.ChooseTest-root-PointGraph.dot -o ChooseTest-PointGraph-n100.pdf

Please see if it is fine with this way. I have pused to branch issue-69.

@cyrille-artho
Copy link
Owner Author

OK, we can use this for the time being, but later we should change this back. (All unit tests will share Modbat.isUnitTest to implement this type of feature.)

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

No branches or pull requests

2 participants