初始化时出现初始化错误:NoClassDefFound

时间:2018-11-13 10:17:21

标签: spring spring-boot mono flux reactive

我是Ropi,我的春季靴子项目测试有问题。
问题是:

  

UnknownClassName.initializationError:NoClassDefFound UnknownClassName;

当我使用跳过的测试过程运行项目时,它运行良好。然后我运行测试,它也运行良好。.

但是在我编辑测试后(例如:我仅在测试代码上添加了空格),它给了我一个错误。

然后,当我编辑我的工作代码时(例如:我只在我的编码工作中添加空间),它给了我非法错误。

我的测试代码:

import com.emerio.rnd.otp.service.OtpService;

import org.hamcrest.core.IsNot;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.hamcrest.Matchers.*;

@RunWith(SpringRunner.class)
// @SpringBootTest
@WebFluxTest
public class OtpApplicationTests {

    private Integer OTP1;
    private Integer OTP2;


    @Autowired
    private WebTestClient webTestClient;
    private OtpService otpService = new OtpService();

    @Before
    public void setup() throws Exception {
        OTP1 = otpService.generateOTP("admin");
        OTP2 = otpService.generateOTP("admin1");
    }

    // @WebFluxTest(OtpController.class)
    @Test
    public void generateOTPSuccess() throws Exception
    {

        webTestClient.get().uri("/generateotp/admin")
            .exchange()
            .expectStatus().isOk()
            .expectBody()
            .jsonPath("$.data.otp").isNotEmpty();
    }

    @Test
    public void generateOTPUsernameNotExist() throws Exception
    {

        webTestClient.get().uri("/generateotp/null")
            .exchange()
            .expectStatus().isBadRequest();
    }

    // @Test
    // public void generateNewOTP() throws Exception
    // {

    //     webTestClient.get().uri("/generateotp/admin1")
    //         .exchange()
    //         .expectStatus().isOk()
    //         .expectBody()
    //         .jsonPath("$.[otp != "+OTP2+"]");
    // }

    @Test
    public void validateOTPSuccess() throws Exception
    {

        webTestClient.get().uri("/validateotp/admin/"+OTP1.toString())
            .exchange()
            .expectStatus().isOk();
    }

    @Test
    public void validateOTPfailed() throws Exception
    {

        webTestClient.get().uri("/validateotp/admin/981981")
            .exchange()
            .expectStatus().isBadRequest();
    }

    @Test
    public void validateOTPWithChar() throws Exception
    {

        webTestClient.get().uri("/validateotp/admin/AS1213")
            .exchange()
            .expectStatus().isBadRequest();
    }

    @Test
    public void validateOTPTimeOut() throws Exception
    {
        // Thread.sleep(300010);
        webTestClient.get().uri("/validateotp/admin/981981")
            .exchange()
            .expectStatus().isBadRequest();
    }

我的控制器工作代码

我的控制器:

import com.emerio.rnd.otp.response.Response;
import com.emerio.rnd.otp.service.OtpService;
import com.google.gson.Gson;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
public class OtpController 
{
    private OtpService otpService = new OtpService();
    private Response response = new Response();


    @RequestMapping(method=RequestMethod.GET , value="/generateotp/{username}", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Mono<Object> generateOTP (@PathVariable String username)
    {
        try
        {   
            if (username.equals("null"))
            {
                return Mono.just(new Throwable("error"));
            } else 
            {
                int otp = otpService.generateOTP(username);
                JSONObject jObj = new JSONObject();
                jObj.put("otp", otp);
                return Mono.just(response.loaded(new Gson().fromJson(jObj.toString(), Object.class)));
            }
        } catch (Exception e)
        {
            return null;
        }
    }


    @RequestMapping(method=RequestMethod.GET , value="/validateotp/{username}/{otp}", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Mono<Response> validateOTP (@PathVariable String username, @PathVariable String otp)
    {
        try
        {
            Integer otpCache = otpService.getOTP(username);

            if (!otpCache.toString().equals(otp))
            {
                System.out.println("cache awal : " + otpCache);
                System.out.println("cache : " + otp);
                return Mono.just(response.loaded("otp not valid"));
            } else 
            {
                otpService.clearOTP(username);
                return Mono.just(response.loaded("otp valid"));
            }
        } catch (Exception e)
        {
            return null;
        }
    }

我的OTP服务:

package com.emerio.rnd.otp.service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import org.springframework.stereotype.Service;

@Service
public class OtpService
{
   private static final Integer EXPIRE_MINS = 5;

   private LoadingCache<String, Integer> otpCache;

   public OtpService()
   {
       super();
       otpCache = CacheBuilder.newBuilder()
                   .expireAfterWrite(EXPIRE_MINS, TimeUnit.MINUTES).build(new CacheLoader<String,Integer>()
                   {
                       public Integer load (String key)
                       {
                           return 0;
                       }
                   });
   }

   public int generateOTP (String key)
   {
       Random random = new Random();
       int otp = 100000 + random.nextInt(900000);
       otpCache.put(key, otp);
       return otp;
   }



   public int getOTP (String key)
   {
       try
       {
           return otpCache.get(key);
       } catch (Exception e)
       {
           return 0;
       }
   }

   public void clearOTP (String key)
   {
       otpCache.invalidate(key);
   }

}

0 个答案:

没有答案