0
点赞
收藏
分享

微信扫一扫

记录一次很坑的报错:java.lang.Exception: The class is not public.


文章目录

  • ​​1、Docker中运行的服务​​
  • ​​2、遇到问题​​
  • ​​第一个问题是项目直接启动失败?​​
  • ​​第二个问题是项目启动后获取不到注入的bean?​​
  • ​​第三个问题就是测试单元的引入问题?​​
  • ​​第四个问题就是公共类和方法?​​
  • ​​3、这里是完整的代码部分(正确)​​
  • ​​4、成功测试通过​​


前言
在进行单元测试连接虚拟机中的​​​ elasticsearch​​,出现了一堆小问题,前前后后花了我一个多小时

1、Docker中运行的服务

记录一次很坑的报错:java.lang.Exception: The class is not public._开发语言

2、遇到问题

原因各有千秋,这里只给出我遇到的情况

第一个问题是项目直接启动失败?

原因:springboot的版本和springcloud的版本不一致导致

记录一次很坑的报错:java.lang.Exception: The class is not public._junit_02

第二个问题是项目启动后获取不到注入的bean?

需要添加这个注解:​​@RunWith(SpringRunner.class)​

第三个问题就是测试单元的引入问题?

//注释掉import org.junit.jupiter.api.Test; 使用下面这句
import org.junit.Test;

第四个问题就是公共类和方法?

要在类上和方法上添加public

3、这里是完整的代码部分(正确)

package com.atguigu.gulimall.search;


import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.search.config.GulimallElasticSearchConfig;
import lombok.Data;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallSearchApplicationTests {

@Autowired
private RestHighLevelClient client;

@Test
public void indexData() throws IOException {
IndexRequest indexRequest = new IndexRequest("users");
indexRequest.id("id");
User user = new User();
user.setUserName("lisi");
user.setAge(18);
user.setGender("男");
String jsonString = JSON.toJSONString(user);
indexRequest.source(jsonString, XContentType.JSON);

//执行
IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);

//提取
System.out.println(index);


}

@Data
class User{
private String userName;
private String gender;
private Integer age;

}

@Test
public void contextLoads() {
System.out.println(client);
}

}

4、成功测试通过

记录一次很坑的报错:java.lang.Exception: The class is not public._java_03


记录一次很坑的报错:java.lang.Exception: The class is not public._elasticsearch_04


举报

相关推荐

0 条评论