I am using OracleJVM with Intellij remote debugging. I am not doing any DCEVM fancy stuff. My code:
public static String test() {
String data; //new code
if (some condition){
//...
data = "abc"; //new code
//...
}
}
After making the change, recompile the class and verifying 'hotswap' finished successfully, the static method is re-ran but debugger variables window says
Cannot find local variable 'data'
Related
I'm trying to load values from environment variables when running a BenchmarkDotNet project from the command line. I'm passing my environment variables using --envVars key:value. However, they're not being picked up by the benchmark.
Here's some sample code:
using BenchmarkDotNet.Attributes;
[MemoryDiagnoser]
[ReturnValueValidator(failOnError: true)]
public class RedisBenchmark
{
[GlobalSetup]
public void GlobalSetup()
{
_redisService.Servers = System.Environment.GetEnvironmentVariable(Constants.REDIS_SERVERS) ?? "";
if (string.IsNullOrEmpty(_redisService.Servers)) _redisService.Servers = "NO SERVERS SPECIFIED";
}
private static RedisService _redisService = new();
[Benchmark(Description = "Write and Read to Redis")]
public void WriteAndReadRedis() => _redisService.WriteAndRead();
}
Notice the "NO SERVERS SPECIFIED" string above. When my RedisService runs and outputs its connection string as the benchmark warms up, it outputs that string, so I know that GlobalSetup ran, but that it wasn't able to find or read the environment variable.
Is there something I'm missing?
Some background and discussion here as well:
https://github.com/dotnet/BenchmarkDotNet/issues/2156
As of this writing, there is a bug affecting the [ReturnValueValidator] and it looks like it skips running GlobalSetup. As a temporary fix you can remove the [ReturnValueValidator] and it should work.
When debugging in IDE, how does the IDE know how to calculate the watch value without changing the environment (writing to file, writing result to DB)?
Your observation cannot be generalized. An IDE typically makes changes during debugging, especially if a property has a side effect.
Visual Studio
The following C# code:
using System;
namespace EvaluateChangesValue
{
class Program
{
static void Main()
{
var program = new Program();
Console.WriteLine(program.Value);
Console.ReadLine();
Console.WriteLine(program.Value);
Console.ReadLine();
}
private int member;
private int Value => member++;
}
}
Set a breakpoint at the first ReadLine(), then add program.Value to the watch window and see how the value gets increased due to the member++ statement.
Eclipse
In Java and Eclipse, it's a bit harder to make the same proof because for these reasons:
In Java it's more clear whether you call a method or access a field.
You need the "Expressions" window, which is not available by default
Re-evaluation needs user interaction
The code is similar to C#:
public class Program {
public static void main(String[] args)
{
Program p = new Program();
System.out.println(p.member);
System.console().readLine();
System.out.println(p.member);
System.console().readLine();
}
private int member;
public int getMember()
{
return member++;
}
}
And the screenshot:
Im started to learn TDD just now. And i have some troubles with testing my controllers. So, i will try to explain.
I have a controller:
public AccountController(IStoreService storeService)
{
_storeService = storeService;
}
public virtual ActionResult RenderBalance()
{
var model = _storeService.GetStorePageBalanceModel();
return PartialView("MyControl", model);
}
Here i want to test my RenderBalance action:
public class when_balance_renders
{
private static Mock<IStoreService> storeService = new Mock<IStoreService>();
private static AccountController controller;
private static ActionResult result;
private Establish context = () =>
{
controller = new AccountController(storeService.Object);
result = controller.RenderBalance();
};
private It should_be_not_null_result = () => { result.ShouldNotBeNull(); };
}
But this code doesn't work. I have this error on debug mode:
Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format.
How may i fix it? And can you give me some recommendations about testing controllers.
Thanks, Nogin Anton.
If you are just starting out with TDD, try a simpler approach like classical TDD as pointed out here http://martinfowler.com/articles/mocksArentStubs.html
Also if you have this error Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format.
There is something very basic setup wrong. Remove lines of code until you can at least compile, then move forward from there.
Trying to do something simple -
I have a set of statements to clear browser cookies:
public void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("10000");
selenium.deleteAllVisibleCookies();
}
Now, if I use this function in a test script (using TestNG), calls to this work perfectly. However, if I moved this function to a separate class and change the declaration to include "static", the "selenium" keyword is not recognized.
In a configuration class (say configClass),
public static void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}
Now, in my test script, if I call configClass.clearCookies();, I get a runtime error
I tried declaring DefaultSelenium selenium = new DefaultSelenium(null);, in the clearCookies() function, but that too results in a runtime error.
I do have the import com.thoughtworks.selenium.*; import in my configClass.
Any pointers would be appreciated. Thanks.
You can do two things.
Refer to the same selenium object in both the classes i.e. in configClass and the class you are calling configClass.clearCookies().
or else
send selenium object to the clearCookies. So the code would be like this
public static void clearCookies (DefaultSelenium selenium) {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}
OK, here's a good one (I think) - I'm working on an application with lots (far too many) dependency dlls, created by a team of developers. I'm trying to debug just one assembly, but the console output is 'polluted' by the Console.WriteLines and Debug.WriteLines left scattered around the code.
Is there anyway I can work out exactly which assembly a given line is coming from, so I can get the author to clean up their source?
UPDATE If you're also experiencing this kind of issue, note that there is another potential source of output messages which is any breakpoints with 'When hit' set to print a message. Having said which, this is a VERY cool feature, which can prevent the kind of problems I was having above.
Yes - replace Console.Out. Use Console.SetOut after creating a TextWriter which not only dumps the requested data to the original console, but also dumps a stack trace (and timestamp, and the requested data) to a file.
Here's some code, adapted from Benjol's answer:
(Note: you will want to adapt this code depending on whether you want a stack trace after each write, or after each writeline. In the code below, each char is followed by a stack trace!)
using System.Diagnostics;
using System.IO;
using System.Text;
public sealed class StackTracingWriter : TextWriter
{
private readonly TextWriter writer;
public StackTracingWriter (string path)
{
writer = new StreamWriter(path) { AutoFlush = true };
}
public override System.Text.Encoding Encoding
{
get { return Encoding.UTF8; }
}
public override void Write(string value)
{
string trace = (new StackTrace(true)).ToString();
writer.Write(value + " - " + trace);
}
public override void Write(char[] buffer, int index, int count)
{
Write(new string(buffer, index, count));
}
public override void Write(char value)
{
// Note that this will create a stack trace for each character!
Write(value.ToString());
}
public override void WriteLine()
{
// This is almost always going to be called in conjunction with
// real text, so don't bother writing a stack trace
writer.WriteLine();
}
protected override void Dispose(bool disposing)
{
writer.Dispose();
}
}
To use this for logging both Console.WriteLine and Debug.WriteLine to a file, make calls like this as early as possible in your code:
var writer = new StackTracingWriter(#"C:\Temp\ConsoleOut.txt");
Console.SetOut(writer);
Debug.Listeners.Add(new TextWriterTraceListener(writer));
Note that this currently doesn't also write to the original console. To do so, you'd need to have a second TextWriter (for the original console) in StackTracingWriter, and write to both places each time. Debug will however continue to be written to the original console.
Download Reflector and you can open up the mscorlib assembly, add your application's assemblies, then right click on the Console class and click Analyze and you can show all methods that reference the Console class.