@Rule>必须在Kotlin Junit测试中公开ValidationError

时间:2017-11-27 10:49:38

标签: android unit-testing junit kotlin

我尝试使用单元测试规则注释,Android Studio没有突出显示任何错误:

@Rule val htmlManager = HtmlManager()

然而,在执行测试后发生以下错误:

  

org.junit.internal.runners.rules.ValidationError:@Rule   ' htmlManager'必须公开。

如何解决这个问题?

3 个答案:

答案 0 :(得分:25)

解决方案是将@Rule注释应用于属性getter:

@get:Rule
val htmlManager = HtmlManager()

此处提供更多详细信息:https://kotlinlang.org/docs/reference/annotations.html#java-annotations

在我的开源项目中查看修复的测试用例代码:https://github.com/appham/Sharemarks/commit/310c115d5a820be900abc321cc061aeab7af2e5a#diff-5e1e851ef5b9bb333abb96dec3199a94

答案 1 :(得分:5)

您也可以使用@JvmField批注

@Rule @JvmField 
val htmlManager = HtmlManager()

答案 2 :(得分:0)

另一个解决方案是:

<?php
$page = 1; //change number of page here
$url = "https://api.1.tumbex.com/api/tumblr/posts?tumblr=memes&type=posts&page=$page&tag=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
    'Accept: */*',
    'Authorization: Bearer 0fae0f237b33e781a6884295b39c6e903484ef1ee3190bd51f07dd9881bdccbd',
    'content-type: application/json; charset=UTF-8',
    'Referer: https://www.tumbex.com/',
    'Accept: application/json, text/plain, */*',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
    'x-csrf-token: MVMyb2hLTWtQdEJEYjJ0SER1dEwvZz09',
    'x-requested-with: XMLHttpRequest'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($ch); //result is json

$json = json_decode($res, true);
$edan = $json['response']['posts'];

for($i=0; $i<count($edan); $i++){

    $get_post       = $edan[$i];
    $type           = $get_post['detected_type']; //get type

    //$get_photo = $get_post['blocks'][0]['content'][0]['hd'];  -> get image url
    //$get_video = $get_post['blocks'][0]['content'][0]['media']['url']; -> get video url
    //$get_text1 = $get_post['blocks'][0]['content'][0]['text']; -> get text 1
    //$get_text2 = $get_post['blocks'][0]['content'][1]['text']; -> get text 2

    if($type == 'photo'){
        $get_photo  = $get_post['blocks'][0]['content'][0]['hd'];
        echo "<img src='".$get_photo."' height='120' width='160'><br>";
    }

}