无法解析静态方法junit assert。

3

我曾尝试使用Junit 3.8和Junit 4。在阅读了Android文档后得知Android不支持Junit4,我将其降级至3.8。但是,我一直遇到以下错误:

02-17 16:37:27.409: W/dalvikvm(32014): VFY: unable to resolve static method 3467: Lorg/junit/Assert;.assertTrue (Ljava/lang/String;Z)V

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kronosDev.nodeMud"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.kronosDev.nodeMud"
android:label="Kronos Tests" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

        <uses-library android:name="android.test.runner" />

    <activity
        android:name="com.kronosDev.nodeMud.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我确认junit 3.8(和Junit4)作为外部JAR在构建路径上。我已尝试单独包含每个jar,但没有结果。 我的测试代码:

import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestSuite;
//import org.junit.Before;
//import org.junit.Test;

import android.util.Log;

public class NodeMudTest extends TestSuite{

Node root=null;
Node a1=null;
Node a2=null; 
Node a3=null;
Node a4=null;   

//@Before
public void setUp() throws Exception 
{
    Log.i("aaa","in setup for testing node");
    //Node a1=Node(Node parent,String prompt,Map<String,Node>children); 
    Map<String,Node>children=new HashMap<String,Node>();

    children.put("a",a1);
    children.put("b",a2);
    root=new Node(null,"stuff here",children);
    children.clear();
//more code here. omitted for brevity

}

//@Test
public void testRunning()
{
    Log.i("aaa","test running in test class");
    assertTrue("up and running successfully",true);
}

//@Test
public void testAddNode() {
    Log.i("aaa","in add node in test class");
    assertEquals("root number of children correct", 2, root.getChildren().size());
}
}

我错过了什么?


@CarlosRobles,我刚刚尝试运行了一个测试,其中所有的Assert都被注释掉了(用打印语句代替),但我收到了以下错误信息:02-18 08:47:46.712: W/dalvikvm(9330): method Landroid/test/InstrumentationTestRunner$StringResultPrinter;.print incorrectly overrides package-private method with same name in Ljunit/textui/ResultPrinter; 我使用的是Junit3.8和Junit4.1。 - Rilcon42
你的方法名称存在冲突,我建议将 import static org.junit.Assert.*; 改为 import org.junit.Assert.*; 并且将所有函数调用改为使用类名,例如将 assertTrue() 改为 Assert.assertTrue(),同样的方式也适用于打印语句。 - Carlos Robles
@CarlosRobles,当我导入org.junit.Assert时,现在出现了VFY:无法解析静态方法3468:Lorg/junit/Assert;.assertEquals(Ljava/lang/String;JJ)V的错误。 - Rilcon42
抱歉,我之前的评论可能是在你提问后才回答了你的问题。 - Rilcon42
我现在有点迷失了。可能是类没有正确导出吗?看一下这个答案:http://stackoverflow.com/a/21838559/2357411 - Carlos Robles
显示剩余4条评论
2个回答

1

0

您应该删除所有导入(静态和非静态)org.junit包下的内容,并将测试更改为扩展junit.framework.TestCase

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

import junit.framework.TestCase;

import android.util.Log;

public class NodeMudTest extends TestCase {
  private Node root;
  private Node a1;
  private Node a2; 
  private Node a3;
  private Node a4;   

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    Log.i("aaa", "in setup for testing node");
    Map<String,Node>children=new HashMap<String,Node>();

    children.put("a", a1);
    children.put("b", a2);
    root = new Node(null, "stuff here", children);
    children.clear();
    //  more code here. omitted for brevity
  }

  public void testRunning() {
    Log.i("aaa", "test running in test class");
    assertTrue("up and running successfully", true);
  }

  public void testAddNode() {
    Log.i("aaa", "in add node in test class");
    assertEquals("root number of children correct", 2, root.getChildren().size());
  }
}

此外,请仅将一个 JUnit jar 放在您的类路径上。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接