Unit testing my plugin

Hi all,

I am working on a plugin currently and I would like to unit test it. My code does however depend on sponge(for example retrieving a player by UUID), how should I unit test this? Currently I am using JUnit.

You could always create stub classes for whatever Sponge methods you need. In fact that’s probably the best way to get a clean unit test anyway. I haven’t used JUnit myself but I’m sure it supports stubbing.

Yeah, but that would require an awfull load of mocks, since you have to mock the Game class, but also the classes in the game class(The server for example)

That’s true. The other thing you could do is to actually include the sponge jar file as a dependency in your test, that might be easier. Not as clean though, and a lot harder to inject data for specific tests.

Well I did as you first suggested (Mocking everything), and it works quite well, thanks! However, I do get another error now: Could not find org.spongepowered:spongecommon:0.1-SNAPSHOT. Do you now the repository of spongecommon so I can add it to my gradle file?

I don’t know it personally. Someone else might.

You’re probably meant this:

org.spongepowered:spongecommon:1.0

but you should be building against 2.0 or 2.1-SNAPSHOT as that’s what the current implementation is going for.

That still gives the same error error:

package org.spongepowered.common does not exist
import org.spongepowered.common.SpongeGame;

My full build.gradle:

apply plugin: 'java'

version = '1.0'

repositories {
    mavenCentral()
    jcenter()

    maven {
        name 'Sponge Maven repository'
        url 'http://repo.spongepowered.org/maven'
    }

    jar {
        baseName = 'Swordkit'
    }
}

dependencies {
    compile 'org.spongepowered:spongeapi:2.1-SNAPSHOT'
    testCompile 'junit:junit:4.11',
                'org.mockito:mockito-core:1.+',
                'org.powermock:powermock-mockito-release-full:1.6.2',
                'org.spongepowered:spongeapi:2.1-SNAPSHOT'
}

Did you reimport it / regeneratre project files via gradlew ideaor gradlew eclipse?

No clue

Anyway I fixed it by not using spongecommon but spongeapi for the unittests. Thanks for all the help everyone