Autore Topic: Android studio 2 + junit: error method should be public  (Letto 527 volte)

Offline BruceAndDevIT

  • Nuovo arrivato
  • *
  • Post: 12
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    lg l5
  • Sistema operativo:
    windows seven
Android studio 2 + junit: error method should be public
« il: 23 Aprile 2016, 23:43:38 CEST »
0
salve sto svillando una app
e sto usando junit (non roboeletric) per i test il problema e che

da questo codice di test

Codice: [Seleziona]
package UnitTests;

import android.test.suitebuilder.annotation.SmallTest;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
 * Created by on 23/04/16.
 */

@SmallTest
public class MappingActionByTagTest {

    @Test
    void addActionWithHandler()
    {
        //arrange
        MappingActionByTag mact = new MappingActionByTag();
        mact.addActionWithTag("test", new ICommand() {
            public String exec() {
                return "tag_test";
            }
        });


        //act
        String actual = mact.getAction("test").exec();


        //assert
        assertEquals(actual,"tag_test");

    }

}

classe sotto test

Codice: [Seleziona]
package UnitTests;


/**
 * Created by on 23/04/16.
 */
public interface ICommand {
    String exec();

}



package UnitTests;


/**
 * Created by on 23/04/16.
 */
public class CommandContentRecent implements ICommand {

    public String exec() {
        return "";
    }
}


package UnitTests;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by on 23/04/16.
 */
public class MappingActionByTag {
    private Map<String, ICommand> m_commandMap = new HashMap<>();

    public void addActionWithTag(String p_tag, ICommand p_iCommand) {
        m_commandMap.put(p_tag,p_iCommand);
    }

    public ICommand getAction(String p_tag) {
        return m_commandMap.get(p_tag);
    }
}


mi viene dato questo errore che non capisco come risolvere

java.lang.Exception: Method addActionWithHandler() should be public

   at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:96)
   at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:74)
   at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:155)
   at org.junit.runners.BlockJUnit4ClassRunner.validateTestMethods(BlockJUnit4ClassRunner.java:208)
   at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:188)
   at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
   at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
   at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
   at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
   at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
   at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
   at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
   at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
   at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
   at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
   at com.intellij.junit4.JUnit46ClassesRequestBuilder.collectWrappedRunners(JUnit46ClassesRequestBuilder.java:90)
   at com.intellij.junit4.JUnit46ClassesRequestBuilder.getClassesRequest(JUnit46ClassesRequestBuilder.java:51)
   at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:91)
   at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:40)
   at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
   at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:606)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


Process finished with exit code 255


gli altri test passano ...

Offline tonno16

  • Utente storico
  • *****
  • Post: 1197
  • Respect: +58
    • Mostra profilo
  • Dispositivo Android:
    moto g
  • Play Store ID:
    Diego Tonini
  • Sistema operativo:
    OpenSuse
Re:Android studio 2 + junit: error method should be public
« Risposta #1 il: 24 Aprile 2016, 00:39:41 CEST »
0
A me sembra un problema di java. Una classe interna

Offline BruceAndDevIT

  • Nuovo arrivato
  • *
  • Post: 12
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    lg l5
  • Sistema operativo:
    windows seven
Re:Android studio 2 + junit: error method should be public
« Risposta #2 il: 24 Aprile 2016, 07:01:30 CEST »
0
anche a me ma ho fatto una cosa semplice semplice
sto usandp junit 4.12.0

e non so come risolvere