How to use KUnit to test external modules? - unit-testing

I want to write unit tests for an external module that I developed. How to use KUnit to test external modules?

In your module dir/Makefile, add test cases object to obj-m var

Related

blocked on writing unit tests to a presenter that has BoxStore injected

So here is the overview of my project:
module A contains:
- all boxstore data
- boxstore mock for unit tests in module A
module B contains:
- presenter that has BoxStore injected
- presenterTest needs to mock BoxStore
Followed this link to mock BoxStore and it works fine when I wrote unit tests in module A. When it comes to creating the mock in module B I get NoClassDefFoundError which I understand since module B tests don't know about module A test objects.
So I did the following scenarios:
Added in gradle of module B:
sourceSets { test.java.srcDirs += [etc...] }
So that module B would know about mock objects of module A.
Duplicate mock of BoxStore in module B and use it in tests but BoxStore generates a java file under build folder and because of that I'm unable to create my mock since everything depends on MyObjectBox in order to generate a BoxStore.
Both methods failed :(
Any ideas on how I can unblock myself?
If you're using Gradle, take a look at the java-test-fixtures plugin that has been introduced in Gradle 5.6.2. It introduces a new testFixtures source set where you can place various test data for a specific module and new DSL that allows other modules to reference this data in their tests. In your case, I imagine you'd put the test data in module A's testFixtures directory, and reference it from module B:
// module A
dependencies {
testImplementation(testFixtures(project(":module-B")))
}

How do I import unit test library in kotlin interactive?

How do I run kotlin interactive prompt kotlinc and be able to import the junit library ? Now when I do it I get an unresolved reference error:
>>> import org.junit.Test
error: unresolved reference: junit
import org.junit.Test
when you use external libraries, you must tell Kotlin where to find them. Hava a look at kotlinc -help
At first, you must download the junit library (e.g. from http://search.maven.org/remotecontent?filepath=junit/junit/4.12/junit-4.12.jar).
Next, point to that library in your compile command with:
kotlinc -classpath junit-4.12.jar YourClass.kt
There are of course situations where using the plain kotlinc command is used by hand. I can't think of one, but there are some for sure. I strongly suggest you, to have a look into build tools, like Gradle or Maven. They will make your life easier. For example, https://github.com/guenhter/kotlin-unit-testing.git is a gradle repo using Kotlin and Junit.

Grails 2 - Using external configuration in tests

I am not able to access configurations properites from grails-app/conf/Config.groovy in my Unit Tests and Integration Tests.
The properties does not exist in grailsApplication.config object
Using Grails 2.5.1 and IntelliJ IDEA 14
What's the best practice to configure my Unit Tests and Integration Tests with Grails 2?
For unit tests:
this is normal behavior, since this is an external dependency and you should not be relying on it, but stubbing it. UnitTestMixin provides a config variable within your unit test, that you can use to stub the values, i.e. config.myValue = xx
For integration test: grailsApplication.config (just inject grailsApplication in you test) should be accessible as usual. You can change the config values, but do not forget to clean up (i.e. restore to original values) after the test.
Solution 1:
IntelliJ > File menu > Project Structure > Modules
Add grails-app/conf folder to Test Source Folders
Solution 2:
Create a separated config file for running tests.
Put a Config.groovy file in test/unit folder and another Config.groovy file in test/integration folder.
The properties will be loaded into grailsApplication.config object

Do I really need a unit test project for mstest to discover my unit tests?

I want to have the following architecture:
- Stories (folder)
- CreateUser (class library)
Dependencies
View
ViewModel
Repository
Tests
- ShareStatusUpdate (class library)
Dependencies
View
ViewModel
Repository
Tests
I do not want to create a separate test project for my unit tests.
Instead, I would rather couple my test to my class library as a first-class citizen.
Is it possible to add test classes to a class library and have the ability to execute those tests?
Try modify your .csproj to add this ProjectTypeGuid for test projects: {3AC096D0-A1C2-E12C-1390-A8335801FDAB}.

Can I configure Grails to see test classes in my plugin?

I have a large grails project split into several 'in place' plugins to keep things nice and modular. One of my plugins, called 'core services', contains all my domain classes, and also some nice set up work in an abstract test class (adding mock domain instances, etc) that is shared by a number of unit tests.
This is all great for unit tests that also live in the plugin, but I'd like to use that abstract test class to set up mock data in other grails projects' tests that uses that plugin. It doesn't seem that plugins' test classes are included on the classpath when running tests. Is there a way I can tell Grails to include them?:
//this abstract test class is in the core service plugin's test directory. My IDE has no problem with this import
import com.myproject.coreservices.service.AbstractGiftInstanceRelatedUnitTest
//this test class is in the project that uses the plugin
class ClaimGiftControllerTests extends AbstractGiftInstanceRelatedUnitTest {
.. my tests
}
And the output (non important parts removed):
| Error Compilation error compiling [unit] tests: startup failed:
...../test/unit/com/myproject/ClaimGiftControllerTests.groovy: 3: unable to resolve class com.myproject.coreservices.service.AbstractGiftInstanceRelatedUnitTest
# line 3, column 1.
import com.myproject.coreservices.service.AbstractGiftInstanceRelatedUnitTest
^
You can put your AbstractGiftInstanceRelatedUnitTest into the src/groovy folder of your plugin, instead of the test folder. That way you can include it in the test cases of the plugin and your other projects.