【问题标题】:unable to resolve static method junit assert无法解析静态方法junit assert
【发布时间】:2014-03-17 09:24:20
【问题描述】:

我尝试过同时使用 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 在构建路径上。我试过单独包括每个罐子,但没有结果。 我的测试代码:

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());
}
}

知道我错过了什么吗?

【问题讨论】:

  • 您是否尝试过使用Assert.assertTrue() 来检查它是否与静态导入有关?
  • @CarlosRobles,我只是尝试在所有断言注释掉的情况下运行测试(用打印语句代替),我得到了错误:02-18 08:47:46.712: W/dalvikvm( 9330): 方法 Landroid/test/InstrumentationTestRunner$StringResultPrinter;.print 错误地覆盖了 Ljunit/textui/ResultPrinter 中同名的包私有方法;使用 Junit3.8 和 Junit4.1
  • 您与方法名称有冲突,我会将import static org.junit.Assert.*; 更改为仅import org.junit.Assert.*; 并将所有函数调用更改为使用类名,例如将assertTrue() 更改为Assert.assertTrue()和相同的打印语句
  • @CarlosRobles,我现在得到:VFY:无法解析静态方法 3468:Lorg/junit/Assert;.assertEquals (Ljava/lang/String;JJ)V 当我导入 org.junit.Assert .
  • @CarlosRobles,试图澄清我这样做了:使用 import junit.framework.assert;给了我与使用 junit 3.8 之前相同的错误。如果我切换到 junit4.1(这让我可以使用 import org.junit.Assert.*;)我得到:VFY:无法解析静态方法 3469:Lorg/junit/Assert;.assertEquals (Ljava/lang/String;JJ )V

标签: java android junit junit4 junit3


【解决方案1】:

您应该删除 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。

【讨论】:

    【解决方案2】:

    我认为你应该使用 junit.framework.Assert 而不是 org.junit.Assert。你可以看看这个http://developer.android.com/reference/junit/framework/Assert.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      相关资源
      最近更新 更多